0

我想在MEDIA_BUTTON单击时调用一个函数。但是在扩展BroadcastReceiverConstructor中被调用但onReceive函数没有被调用。

我已经通过蓝牙设备对此进行了测试,因为它没有任何影响。这样

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>

我如何BroadcastReceiver在 MainActivity.java 中注册

    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 的答案,但无法解决这个问题。

4

1 回答 1

0

这已经很晚了,但是它不起作用的原因是Android 的更新。从 Android 8.0 及更高版本开始,静态广播接收器将不起作用。您必须在启动应用程序后动态注册它。

于 2021-05-24T12:19:35.257 回答