Broadcast receiver highest priority not working
Asked Answered
B

4

10

I'm doing an application using ACTION_MEDIA_BUTTON handler, but it appears it is always intercepted by MX Player or Apollo and I get no Intent

I've tried both 1000 and 2147483647 priority set in tag and directly after constructor with setPriority

Applications works when no MX Player or Apollo is present

I've also tried using Headset Interceptor app from google play, I tried to deny events to MX Player with Autostarts application - nothing helps

in onCreate:

IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.setPriority(1000);
registerReceiver(receiver, filter);

in Receiver

@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
        // NEVER REACHES HERE WHEN MX PLAYER PRESENT. WORKS IF NOT

in manifest

<receiver
    android:name="BCreceiver"
    android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.MEDIA_BUTTON" />
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>
Burp answered 11/9, 2013 at 6:47 Comment(3)
A code sample would be helpful.Sendai
added code samples, but they are pretty standardBurp
Please use android:priority="999" or less; SYSTEM_HIGH_PRIORITY Constant Value: 1000 Applications should never use filters with this or higher priorities.Rodgerrodgers
B
2

To capture headset button one should register receiver in media too in onCreate in Activity

AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), BCreceiver.class.getName()));
Burp answered 11/9, 2013 at 12:1 Comment(2)
registerMediaButtonEventReceiver has been deprecated. The docs say to use MediaSession.setMediaButtonReceiver() instead.Disestablish
to clarify my previous comment, registerMediaButtonEventReceiver() was only deprecated as of v21.Disestablish
T
15

Refer to the line "The value must be greater than -1000 and less than 1000." from below link, highest priority is 999 not 1000.

http://developer.android.com/guide/topics/manifest/intent-filter-element.html

Thurgau answered 26/12, 2014 at 6:47 Comment(2)
That wording no longer exists in the documentation.Amsterdam
@Amsterdam Refer this: developer.android.com/reference/android/content/… The constant value for this is 1000 and it also says "Applications should never use filters with this or higher priorities." So the wordings might have changed but the limit is -999 to 999 only.Samy
P
3

Even though this a little old question, I am adding my findings, so that it will help new visitors.

To receive Intent.ACTION_MEDIA_BUTTON broadcast registering intent from code is not required. Documentation says intent has to be registered in the manifest. Could not get it working from registering from code.

Use registerMediaButtonEventReceiver

Setting priority in manifest android:priority="<int value>" works. I used 2147483647 and was even able to override the stock player. I read winamp is using the highest priority.

Hope this helps.

Portecochere answered 15/5, 2016 at 10:20 Comment(0)
B
2

To capture headset button one should register receiver in media too in onCreate in Activity

AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), BCreceiver.class.getName()));
Burp answered 11/9, 2013 at 12:1 Comment(2)
registerMediaButtonEventReceiver has been deprecated. The docs say to use MediaSession.setMediaButtonReceiver() instead.Disestablish
to clarify my previous comment, registerMediaButtonEventReceiver() was only deprecated as of v21.Disestablish
S
1

First of all, you shouldn't register the receiver in code if it's already mentioned in the manifest. Then, the name of the receiver is invalid, it should either be a full class name, or the shorthand, which will be appended to the application package name. In case if BCreceiver is in the main package, the attribute value should be ".BCreceiver". Last mention is that you shouldn't really change the priority, there is no such thing as intercepting a broadcast in Android (as far as I know), so all BroadcastReceivers subscribed to an action will receive the broadcast when it's fired. Try these fixes and update your question.

Sendai answered 11/9, 2013 at 7:11 Comment(3)
When i disable IntentFilter in onCreate and leave manifest only it stops being called even for HEADSET_PLUG (that was called ok before)Burp
Also please note it WORKS without MX Player and it always intercepts HEADSET_PLUG independently of MX Player presence.Burp
@MaxTheCat, After a short research I've found out that indeed broadcast "consumption" can take place in some circumstances. If the broadcast is ordered and both consuming applications set the highest priority to receive this broadcast, then they'll receive it in arbitrary order. Then it's possible to abortBroadcast(), making it unavailable for the others. So this is probably what happens, and I can't think of any way you can overcome this problem.Sendai

© 2022 - 2024 — McMap. All rights reserved.