如果插入耳机,状态栏中会出现耳机图标。
如何在状态栏中隐藏耳机图标?我知道在应用程序https://play.google.com/store/apps/details?id=com.ligq.ikey&hl=ru
中可行答案 0 :(得分:1)
AudioManager具有可通过反射访问的私有方法。
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));
}
所有声音都传递到耳机而不是耳机
答案 1 :(得分:0)
我修改了上面的一点以使它工作。它的工作非常棒,我非常感谢user2930077的帮助。我真正需要添加到代码中的唯一方法是更改设备类型,而是输入文字值。 4 =带麦克风的耳机和8 =没有麦克风的耳机。第二个值是开/关。如果您想要它,请将其设置为1,0,如果您想要它关闭。
我将附上我如何工作,我希望将来可以帮助某人。再次感谢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();
}