-3

If headset was plugged an icon of headset is appeared in status bar.

How to hide headset icon in status bar ? I know that is possible in application https://play.google.com/store/apps/details?id=com.ligq.ikey&hl=ru

user2930077
  • 135
  • 2
  • 9

2 Answers2

1

AudioManager has private method which is accessible using reflection.

AudioManager manager = (AudioManager) aContext.getSystemService(Context.AUDIO_SERVICE);
    try {
        int deviceType = AudioSystem.DEVICE_OUT_WIRED_HEADSET;
        // turn off headset
        int state = 0;
        Class<?> cl = Class.forName("android.media.AudioManager");
        Method method = cl.getMethod("setWiredDeviceConnectionState", new Class[] {
                Integer.TYPE, Integer.TYPE, String.class });
        method.invoke(manager, new Object[] { deviceType, state, "device" });
    } catch (Exception e) {
        iLog.error("disableHeadset, exception {}", ExceptionUtils.getStackTrace(e));
    }

All voice passes to earpiece instead headset

user2930077
  • 135
  • 2
  • 9
0

I modified the above a little bit to make it work. It works amazing and I am so thankful to user2930077 for their help. The only thing I really needed to add to the code was to change the device type, I entered literal values instead. 4 = headphones with a microphone and 8 = headphones without a mic. The second value is for on/off. If you want it on, set it at 1, 0 if you want it off.

I will attach how I got it to work as well and I hope it helps someone in the future. Thank you again user2930077!!!!

   AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    Class<?> cl = null;
    try {
        cl = Class.forName("android.media.AudioManager");
        setDeviceConnectionState = cl.getMethod("setWiredDeviceConnectionState", new Class[] {
                Integer.TYPE, Integer.TYPE, String.class });
        //4 is for the headset with microphone 8 is for headset without microphone
        //0 is for off and 1 is for on
        setDeviceConnectionState.invoke(manager, new Object[] { 4 , 0, "device" });
        setDeviceConnectionState.invoke(manager, new Object[] { 8 , 0, "device" });
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
Toby
  • 141
  • 3
  • 12