我最近购买了蓝牙耳机http://www.plantronics.com/middle-east/product/ml20,并且一直试图按下呼叫按钮以触发识别器内容。但到目前为止,我的所有尝试都失败了。考虑到我的耳机可能很古怪并且没有按预期运行,我试图拦截触摸耳机长按时手机上的语音拨号器的任何动作,再次导致失败。下面是我正在使用的代码,根据我读过的所有其他线程应该正常工作。我错过了什么吗? ACTION_VOICE_COMMAND不是触发语音拨号器的原因吗?
private MediaButtonIntentReceiver receiver = new MediaButtonIntentReceiver();
private IntentFilter filter1 = new IntentFilter(Intent.ACTION_VOICE_COMMAND);
private IntentFilter filter2 = new IntentFilter(Intent.ACTION_CALL_BUTTON);
private IntentFilter filter3 = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
private IntentFilter filter4 = new IntentFilter(Intent.ACTION_POWER_DISCONNECTED);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speech_text = (EditText) findViewById(R.id.voiceInput);
filter1.setPriority(100000);
filter2.setPriority(100000);
filter3.setPriority(100000);
filter4.setPriority(100000);
this.registerReceiver(receiver, filter1);
this.registerReceiver(receiver, filter2);
this.registerReceiver(receiver, filter3);
this.registerReceiver(receiver, filter4);
}
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
/*String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
// do something*/
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_LONG).show();
Log.v("TEST", "I am logging something informational!");
//}
//abortBroadcast();
}
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
logcat的一些示例输出
01-17 18:36:03.977: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:04.977: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:05.477: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:05.978: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:06.477: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:10.967: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:11.768: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:12.608: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:13.238: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:13.948: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:14.618: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null
01-17 18:36:15.278: E/bt-rfcomm(6240): PORT_DataInd, p_port:0x6a90b668, p_data_co_callback is null