我在我的服务中制作这些代码:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.MEDIA_BUTTON");
intentFilter.setPriority(2147483647);
registerReceiver(broadcastReceiver,intentFilter);
那就是接收者
public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
playNext(); // play the next music
}
};
但我从未接收过广播,我不知道为什么。 每次我点击耳机,下面都会显示
I / MediaButtonIntentReceiver:句柄动作:android.intent.action.MEDIA_BUTTON I / MediaButtonIntentReceiver:action = 1,repeatCount = 0,command = togglepause,keycode = KEYCODE_HEADSETHOOK
Rocco是对的。
答案 0 :(得分:0)
您可能需要获得许可:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
答案 1 :(得分:0)
如果您班上有多个接收者: 你注册&#34; broadcastReceiver&#34;,但命名为&#34; broadcastReceiver1 *&#34;