Broadcast receiver highest priority not working

20.9k views Asked by At

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>
4

There are 4 answers

2
Max The Cat On BEST ANSWER

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()));
3
Egor On

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.

2
Vipin Sharma On

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

0
Raghav Pete On

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.