ACTION_MEDIA_BUTTON 接收器在尝试了几乎所有我能做的事情后没有被调用

问题描述

我想在点击 MEDIA_BUTTON 时调用一个函数。但是在扩展的 BroadcastReceiver 类中,Constructor 被调用但 onReceive 函数没有被调用。

我已经在 BluetoothDevice 上对此进行了测试,因为它没有任何影响。 by this

Manifest.xml 文件中的内容

<uses-permission android:name="android.permission.BLUETOOTH" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.NotificationManager">
    <meta-data
        android:name="com.google.android.actions"
        android:resource="@xml/actions" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".NotificationService"
        android:enabled="true"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

    <receiver android:name=".MediaButtonIntentReceiver">
        <intent-filter android:priority="1000000000">
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

</application>

我如何在 MainActivity.java 中注册 BroadcastReceiver

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    registerReceiver(r,filter);

MediaButtonIntentReceiver.java 中的内容

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.widget.Toast;
    public class MediaButtonIntentReceiver extends BroadcastReceiver{
        private static final String TAG = "MediaButtonReceive";
        MediaButtonIntentReceiver(){
            super();
            Log.e(TAG,"Constructor Called"); //Also tried without Constructor.
        }

        public void onReceive(Context context,Intent intent) {
            Log.e(TAG,"In onReceive");       // It won't even print this.

            String intentAction = intent.getAction();
            if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
                KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

                if (event == null) {
                    return;
                }
                int keycode = event.getKeyCode();
                int action = event.getAction();

                if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                    if (action == KeyEvent.ACTION_DOWN) {
                        Log.e(TAG,"Button Pressed");    //Call function here.
                        Toast.makeText(context,"Button pressed !!",Toast.LENGTH_SHORT).show();
                        if (isOrderedBroadcast()) {
                            abortBroadcast();
                        }
                    }
                }
            }
        }
    }

我已经参考了至少 8 个 Stack 的答案,但无法解决此问题。

解决方法

这已经晚了,但它不起作用的原因是Android's Update。 从 Android 8.0 开始,静态广播接收器将无法工作。 您必须在启动应用程序后动态注册它。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...