我有一个Android应用,需要对耳机媒体按钮实施自己的操作。我在服务类中实现了自己的MediaButtonReceiver。
我已经尝试过但无法使用的内容:
AndroidManifest:
<receiver android:name=".PlayerService$MyMediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
在我的PlayerService类中:
public static class MyMediaButtonReceiver extends MediaButtonReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("********* test ***********");
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null) {
Intent broadcastIntent = new Intent("changeStation");
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
break;
}
}
}
}
}
}
为什么不起作用(我在Android 9设备上进行测试),并且在调试窗口中看不到“ ********* test ***********”字符串?
感谢帮助!