我有一段代码可以检测我是否正在接听电话,然后我的应用停止播放音乐并继续播放。如果任何蓝牙设备与我的设备断开连接,我想做同样的事情,我希望它也停止播放。我不知道从哪里开始,请帮助我。谢谢。
这是我的来电代码:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
if (audioOncall) {
resetMetadata();
UpdatetoLogo();
Intent restart = new Intent(getApplicationContext(), MainActivity.class);
startActivity(restart);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
if (audioOncall) {
resetMetadata();
UpdatetoLogo();
Intent restart = new Intent(getApplicationContext(), MainActivity.class);
startActivity(restart);
}
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
audioOncall = audioSignal.isPlaying();
stopOncall();
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
我已经设法让蓝牙工作了,但是它没有听取更改活动,它只接收应用程序加载时的状态...如何更改以下代码以使其听对于bluetooth
活动中的更改?
public void onCreate() {
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Device found
}
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
stopOncall();
resetMetadata();
UpdatetoLogo();
Intent restart = new Intent(getApplicationContext(), MainActivity.class);
startActivity(restart);
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
stopOncall();
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
stopOncall();
}
}
};