Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast receiver highest priority not working

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>
like image 674
Max The Cat Avatar asked Sep 11 '13 06:09

Max The Cat


People also ask

What is the priority of broadcast in Android?

The priority is assigned to the broadcasts using attribute android:priority. With the help of priority, the broadcast with the highest priority will execute first. If two broadcasts are found to have the same priority then there no order would be followed. Ordered broadcasts are also called as Synchronous Broadcasts.

How to use the broadcast receiver in your application?

The two main things that we have to do in order to use the broadcast receiver in our application are: override fun onReceive (context: Context?, intent: Intent?) { Below is the sample project showing how to create the broadcast Receiver and how to register them for a particular event and how to use them in the application.

What are the different types of broadcast receivers in Android?

There are broadly two types of broadcast receivers in Android: 1. Ordered Broadcasts Ordered Broadcasts are synchronous broadcasts, and are done in proper order. This order is decided by the android:priority attribute. The broadcast with the highest priority would execute first and broadcasts with the same priority would not follow any order.

How do I register the connectivity_action broadcast on Android?

Also, apps targeting Android 7.0 and higher must register the CONNECTIVITY_ACTION broadcast using registerReceiver (BroadcastReceiver, IntentFilter) . Declaring a receiver in the manifest doesn't work. Apps can receive broadcasts in two ways: through manifest-declared receivers and context-registered receivers.


2 Answers

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

like image 192
Vipin Sharma Avatar answered Oct 10 '22 11:10

Vipin Sharma


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.

like image 30
Raghav Pete Avatar answered Oct 10 '22 09:10

Raghav Pete