我正在为 Android 操作系统的 4.0.3 (ICS) 版本编写一个 Android 应用程序。问题是我在 BroadcastReceiver 的 onReceive() 方法中没有从 Log.d() 获取输出,这意味着我的应用程序没有正确处理广播。
我已经阅读了无数关于如何在单击 ACTION_MEDIA_BUTTON 时运行代码的问题。当我的代码不起作用时,我什至复制+粘贴代码,只是为了看看它是否会起作用。
我要处理的 ACTION_MEDIA_BUTTON 是耳机上的单个按钮,允许用户接听/结束通话、播放/暂停音乐。 当我点击该按钮时,我的 Nexus S Android 上的音乐播放器开始播放歌曲,而不是我的应用程序处理此按钮。
我没有将我的代码放在另一个类中,也许这就是它不起作用的原因?
这是在 onCreate() 方法中找到的代码(在我编写的代码不起作用后,我从网站上复制了这个特定代码):
IntentFilter mediaButtonFilter = new IntentFilter(
Intent.ACTION_MEDIA_BUTTON);
mediaButtonFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
BroadcastReceiver brMediaButton = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("Event", "Media button!");
this.abortBroadcast();
KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if(key.getAction() == KeyEvent.ACTION_UP) {
int keycode = key.getKeyCode();
if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
Log.d("TestApp", "Next Pressed");
} else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
Log.d("TestApp", "Previous pressed");
} else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
Log.d("TestApp", "Head Set Hook pressed");
}
}
}
};
registerReceiver(brMediaButton, mediaButtonFilter);
我真正需要测试的只是 KEYCODE_HEADSETHOOK,但在那里有其他代码进行测试并没有什么坏处,一旦我可以让一切正常工作,我会修复它。
在我的 list 中:
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
我最初认为这可能是权限问题,因为我没有为此指定任何权限,但是我没有收到任何错误消息。
就像我之前说的,我已经尝试了很多变体。一个例子是使用此链接中的代码 broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android与 CommonsWare 的更正。但是,我再次对其进行了修改,使其不在单独的类中。
提前感谢您的帮助。
最佳答案
我在装有 Android 4.4.2 的三星 Galaxy S5 上进行了测试。那么什么是重要的,什么是其他帖子中没有提到的:
- 在 application 标签内但在每个 activity 标签外的 AndroidManifest.xml 中注册接收器。
- 您的接收器Broadcastreceiver需要是公开的和静态的
- 一个 Activity 需要注册一个MediaButtonEventReceiver来监听按钮按下
好的,这里有一些示例代码:
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mReceiverComponent = new ComponentName(this,YourBroadcastReceiver.class);
...
mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);
...
// somewhere else
mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);
这里是接收器:
public static class YourBroadcastReceiver extends BroadcastReceiver{
// Constructor is mandatory
public MediaBroadcastReceiver ()
{
super ();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Log.i (TAG_MEDIA, intentAction.toString() + " happended");
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
Log.i (TAG_MEDIA, "no media button information");
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
Log.i (TAG_MEDIA, "no keypress");
return;
}
// other stuff you want to do
}
}
这里是 manifest fragment 。如果需要,为 Intent 过滤器添加优先级,但我不需要:
<application>
<receiver android:name="OuterClass$YourBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity> ... </activity>
</application>
供引用:
关于java - ACTION_MEDIA_BUTTON 的广播接收器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177417/