Alternative to the deprecated AudioManager.isWiredHeadsetOn?

后端 未结 6 1493
暗喜
暗喜 2021-02-13 20:47

The method AudioManager.isWiredHeadsetOn() is deprecated from api level 14, how do we now detect if a wired headset is connected?

6条回答
  •  余生分开走
    2021-02-13 20:56

    IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    Intent iStatus = context.registerReceiver(null, iFilter);
    boolean isConnected = iStatus.getIntExtra("state", 0) == 1;
    

    I do know this code can be used on which andorid platform version. It does not work for me on Android 8. Result iStatus is null.

提交回复
热议问题