如何在Android中调用时监听音频路由的变化

时间:2014-02-25 23:09:15

标签: android audio bluetooth call

我似乎无法弄清楚如何检测用户何时更改通话时音频路由的方式。具体地,监听用户何时在通过蓝牙耳机路由的音频和设备耳机之间切换。有人可以指导我一个很好的例子吗? 感谢。

1 个答案:

答案 0 :(得分:2)

在onCreate或onStartCommand

MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
registerReceiver(myReceiver, filter);

在onDestroy

unregisterReceiver(myReceiver);

BroadcastReceiver类

private class MyBroadcastReceiver extends BroadcastReceiver {       
    @Override 
    public void onReceive(Context context, Intent intent) {         
        if(intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)){
            int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,-1);
            switch(state){
            case BluetoothHeadset.STATE_AUDIO_CONNECTED:
                Log.d(TAG,"Bluetooth connected");
                break;
            case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                Log.d(TAG,"Bluetooth disconnected");
                break;
            }
        }   
    }   
}