前言
由于最近项目遇到,蓝牙手表设备在不配对的情况下监听按键给出相应的控制回应,所以研究了下
网上找了很多不过对于现在来说,很多手机设置没有反应,这里给出一个比较统一的方法
项目需求如下图:
项目需求
方法如下:
*这里主要是为了控制的实现其他的不多说,直接上代码,只是记录下以后也许还会用到
private long eventtime = 0; private AudioManager vAudioManager = null; //此处在onCreate方法中初始化 eventtime = SystemClock.uptimeMillis(); vAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //这是播放或者暂停 if (vAudioManager.isMusicActive()){ Toast.makeText(getApplicationContext(), "有音乐在播放---暂停", Toast.LENGTH_SHORT).show(); pauseMusic();//暂停 }else { Toast.makeText(getApplicationContext(), "无音乐在播放--开始", Toast.LENGTH_SHORT).show(); playMusic();//播放 }
*主要控制代码
/** * 暂停 */ private void pauseMusic() { if (eventtime<=0)return; Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null); Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); sendOrderedBroadcast(upIntent, null); } /** * 播放 */ private void playMusic() { if (eventtime<=0)return; Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null); Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); sendOrderedBroadcast(upIntent, null); } /** * 上一曲 */ private void lastMusic() { if (eventtime<=0)return; Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null); Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); sendOrderedBroadcast(upIntent, null); } /** * 下一曲 */ private void nextMusic() { if (eventtime<=0)return; Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0); downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); sendOrderedBroadcast(downIntent, null); Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0); upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); sendOrderedBroadcast(upIntent, null); }
下面这个是控制系统媒体音量键的加减
// 调低音量 vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); // 调高音量 vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对呐喊教程的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。