headset button 的使用

本文介绍如何在Android应用中注册媒体按钮接收器以监听物理按键事件,如播放、暂停、下一曲等,并展示了如何通过自定义BroadcastReceiver实现对不同按键事件的响应。

android.permission.BLUETOOTH 权限

MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE);
registerReceiver(mMediaButtonReceiver, mediaFilter);

 

不要忘记设置优先权

 

public class HardButtonReceiver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

Log.v(“TestApp”, “Button press received”);

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”);

}

}

}

}

 

因为有些按键不一定被映射却要检查

private class MediaSessionCallback extends MediaSession.Callback { private long mLastClickTime = 0; private boolean mDown = false; private PowerManager.WakeLock mWakeLock = null; @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { Context context = MediaPlaybackService.this; if (mWakeLock == null) { PowerManager pm = (PowerManager) context.getSystemService( Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName()); mWakeLock.setReferenceCounted(false); } // hold wakelock for 3s as to ensure button press event full processed. mWakeLock.acquire(3000); KeyEvent event = (KeyEvent) mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT, KeyEvent.class); if (event == null) { return super.onMediaButtonEvent(mediaButtonEvent); } int keycode = event.getKeyCode(); int action = event.getAction(); long eventTime = event.getEventTime(); // single quick press: pause/resume. // double press: next track // long press: start auto-shuffle mode. String command = null; switch (keycode) { case KeyEvent.KEYCODE_MEDIA_STOP: command = MediaPlaybackService.CMDSTOP; break; case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MediaPlaybackService.CMDTOGGLEPAUSE;//为什么将此处的屏蔽打开音乐退到后台就能暂停了 但是进到音乐应用中又不能暂停了;此处屏蔽现象:出现了音乐退到后台不能暂停了 但是进到音乐应用中又能暂停了 //cwl set key can stop music in back if (isPlaying()) { pause(false); mPausedByTransientLossOfFocus = false; } else { play(); } break; case KeyEvent.KEYCODE_MEDIA_NEXT: command = MediaPlaybackService.CMDNEXT; break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: command = MediaPlaybackService.CMDPREVIOUS; break; case KeyEvent.KEYCODE_MEDIA_PAUSE: if(isPlaying()){//chengyuwen add this for bluetooth pause/play button error 20190121 command = MediaPlaybackService.CMDPAUSE; }else{ command = MediaPlaybackService.CMDPLAY; } // command = MediaPlaybackService.CMDPAUSE; break; case KeyEvent.KEYCODE_MEDIA_PLAY: if(isPlaying()){//chengyuwen add this for bluetooth pause/play button error 20190121 command = MediaPlaybackService.CMDPAUSE; }else{ command = MediaPlaybackService.CMDPLAY; } // command = MediaPlaybackService.CMDPLAY; break; } if (command != null) { if (action == KeyEvent.ACTION_DOWN) { if (mDown) { if ((MediaPlaybackService.CMDTOGGLEPAUSE.equals(command) || MediaPlaybackService.CMDPLAY.equals(command)) && mLastClickTime != 0 && eventTime - mLastClickTime > LONG_PRESS_DELAY) { mMediaButtonHandler.sendMessage( mMediaButtonHandler.obtainMessage(MSG_LONG_PRESS_TIMEOUT, context)); } } else if (event.getRepeatCount() == 0) { // only consider the first event in a sequence, not the repeat events, // so that we don't trigger in cases where the first event went to // a different app (e.g. when the user ends a phone call by // long pressing the headset button) // The service may or may not be running, but we need to send it // a command. Intent i = new Intent(context, MediaPlaybackService.class); i.setAction(MediaPlaybackService.SERVICECMD); if (keycode == KeyEvent.KEYCODE_HEADSETHOOK && eventTime - mLastClickTime < 300) { i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDNEXT); MusicUtils.startService(context, i); mLastClickTime = 0; } else { i.putExtra(MediaPlaybackService.CMDNAME, command); MusicUtils.startService(context, i); mLastClickTime = eventTime; } mLaunched = false; mDown = true; } } else { mMediaButtonHandler.removeMessages(MSG_LONG_PRESS_TIMEOUT); mDown = false; } } return super.onMediaButtonEvent(mediaButtonEvent); } } case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: command = MediaPlaybackService.CMDTOGGLEPAUSE; break;这样修改导致出现了音乐退到后台不能暂停了 但是进到音乐应用中又能暂停了
最新发布
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值