前言
由于最近项目遇到,蓝牙手表设备在不配对的情况下监听按键给出相应的控制回应,所以研究了下
网上找了很多不过对于现在来说,很多手机设置没有反应,这里给出一个比较统一的方法
项目需求如下图:
项目需求
方法如下:
*这里主要是为了控制的实现其他的不多说,直接上代码,只是记录下以后也许还会用到
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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(); //播放
}
|
*主要控制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/**
* 暂停
*/
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 );
}
|
下面这个是控制系统媒体音量键的加减
1
2
3
4
5
|
// 调低音量
vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
// 调高音量
vAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://www.jianshu.com/p/a1a5a02f7c5f