该方法AudioManager.isWiredHeadsetOn()从API级别14过时,如何如果有线耳机连接我们现在检测?
Answer 1:
该文档的过时消息,指出:
只用它来检查被连接耳机或没有。
所以我想它是好的,继续使用它来检查有线耳机是否被连接,但不检查音频是否被路由到它或在其上播放。
Answer 2:
这是我的解决方案:
private boolean isHeadsetOn(Context context) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am == null)
return false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return am.isWiredHeadsetOn() || am.isBluetoothScoOn() || am.isBluetoothA2dpOn();
} else {
AudioDeviceInfo[] devices = am.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (int i = 0; i < devices.length; i++) {
AudioDeviceInfo device = devices[i];
if (device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET
|| device.getType() == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
|| device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP
|| device.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
return true;
}
}
}
return false;
}
Answer 3:
尝试这种解决方案。 它的工作在我的情况。 可能这可以帮助您!
IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
Intent iStatus = context.registerReceiver(null, iFilter);
boolean isConnected = iStatus.getIntExtra("state", 0) == 1;
Answer 4:
我们必须利用广播接收机寻找蓝牙连接的状态。
这里是一个很好的例子
文章来源: Alternative to the deprecated AudioManager.isWiredHeadsetOn?