MediaButtonIntentReceiver not working in Android 4.0+

后端 未结 2 1465
感动是毒
感动是毒 2020-12-01 21:56

The goal is to intercept broadcasts from the headset, as well as bluetooth eventually, to respond to different types of clicks from the headset to alter the mediaplayer. Thi

相关标签:
2条回答
  • 2020-12-01 22:08

    Just try on jelly on this is working for me :

                 broadcastReceiver = new BroadcastReceiver() {//global BroadcastReceiver
                @Override
                public void onReceive(Context context, Intent intent) {
    
                    String action = intent.getAction();
                    if(action.equals("android.intent.action.MEDIA_BUTTON")){
                        Log.e("test", "ok");
                    }
                };
    

    Intent :

            IntentFilter intentfilterTime = null;
            intentfilterTime = new IntentFilter();  
            intentfilterTime.addAction("android.intent.action.MEDIA_BUTTON");
            registerReceiver(broadcastReceiver, intentfilterTime);
    

    Test from service with media button of Widgetsoid (who emulate BT media buttons). All is working.

    0 讨论(0)
  • 2020-12-01 22:22

    It looks like your broadcast receiver is an inner class to your service? If so, make your broadcast receiver static and in the manifest do this:

    <receiver android:name="MyOuterClass$MediaButtonIntentReceiver" android:enabled="true">
        <intent-filter android:priority="2147483647" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
    

    In Android 3.0+ you must use registerMediaButtonEventReceiver to register the receiver. This uses the AndroidManifest for the IntentFilter. The reason it works in 2.x is because you were registering it with this.registerReceiver() which registered the receiver without the AndroidManifest.

    0 讨论(0)
提交回复
热议问题