首先声明一个广播,需要静态注册,如果不静态注册好像不行,不知道为啥?
<receiver android:name="com.example.musicclient.broadcast.AudioBroadcast">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent keyEvent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
// 获得按键字节码
int keyCode = keyEvent.getKeyCode();
// 按下 / 松开 按钮
int keyAction = keyEvent.getAction();
// 获得事件的时间
long downtime = keyEvent.getEventTime();
switch (keyC