这是清单里面的内容(这是里面
<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
主要活动
private MediaButtonIntentReceiver receiver = new MediaButtonIntentReceiver();
onCreate(Bundle savedInstanceState){
..
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter.setPriority(1000);
registerReceiver(receiver,filter);
..
}
这是 MediaButtonIntentReceiver.class
public class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
KeyEvent v_Event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (v_Event == null) {
return;
}
int v_Action = v_Event.getAction();
if (v_Action == KeyEvent.ACTION_DOWN) {
// do something
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
出于某种原因,每当我启动我的应用程序并单击我的耳机按钮时,我都没有收到任何 Toast 消息。音乐开始播放。我试过寻找我在做什么,但对我来说没有运气。