为何ACTION_MEDIA_BUTTON无法处理事件?
作者:互联网
在关于如何进行use hardware playback control keys to control audio playback的培训部分之后,我创建了一个侦听器类:
public class RemoteControlReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Log.e(TAG, "ACTION_MEDIA_BUTTON");
int keycode = event.getKeyCode();
switch (keycode)
{
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.e(TAG, "KEYCODE_MEDIA_NEXT");
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
Log.e(TAG, "KEYCODE_MEDIA_PLAY_PAUSE");
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.e(TAG, "KEYCODE_MEDIA_PREVIOUS");
break;
default:
}
if (/*KeyEvent.KEYCODE_MEDIA_PLAY*/ 126 == event.getKeyCode()) { // KEYCODE_MEDIA_PLAY undefined for API < 11
Log.e(TAG, "KEYCODE_MEDIA_PLAY");
}
}
}
}
在活动的onCreate()中注册了它:
private AudioManager mAudioManager;
private ComponentName mRemoteControlReceiver;
...
...
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class);
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlReceiver);
并将其注册在清单中:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
但是,按下这些按钮中的任何一个都不会导致LogCat上出现任何日志消息! (无论是否在播放我的媒体时被按下)
我的代码似乎拦截了来自其他媒体播放器的这些事件,但是为什么我看不到任何Log.e()消息?我想念什么?
顺便说一句,当我按下这些按钮中的任何一个时,将显示以下消息:
W/KeyCharacterMap(19801): Can't open keycharmap file
W/KeyCharacterMap(19801): Error loading keycharmap file '/system/usr/keychars/h2w_headset.kcm.bin'. hw.keyboards.131075.devname='h2w headset'
W/KeyCharacterMap(19801): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
解决方法:
您碰巧使用Library Project来实现吗?
如果是这样,您将< receiver>在?库的还是应用程序的?
如果您放入图书馆的图书馆,它将无法使用.您必须将其放置在应用程序的清单中:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
标签:android-audiomanager,android,broadcastreceiver 来源: https://codeday.me/bug/20191013/1909031.html