How to disable the wired headset programmatically in Java

穿精又带淫゛_ 提交于 2019-12-10 17:35:02

问题


Please refer my question here. This question is an extension to that:

How to check and redirect audio between wired headset and speaker phone?

I was able to partially solve my problem by including the permissions

  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

in my Android Manifest for that same piece of code. Now, I am able to enable and disable the phone speakers, but I am not able to turn off my wired headset connection programmatically, when the wired headset is still physically connected to the phone.

Can someone please help me here? Is there a specific intent I can use to disable and enable the wired headset connection?


回答1:


I looked for a solution online and found something

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                case 0:
                    audioManager.setSpeakerphoneOn(true);
                    break;
                case 1:
                    audioManager.setSpeakerphoneOn(false);
                    break;
                default:
                    audioManager.setSpeakerphoneOn(true);
            }
        }
    }
}

Hope it helps

If you want to visit the site , here is the link



来源:https://stackoverflow.com/questions/20949769/how-to-disable-the-wired-headset-programmatically-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!