另一种禁用耳机的方法 - AudioManager不再工作了

时间:2017-01-16 13:16:32

标签: android android-audiomanager

我之前能够为其他设备禁用耳机,但现在我正在测试不同的设备,我调用的课程将无效。我假设我在不再工作之前的方式,使用AudioManager方法:setWiredDeviceConnectionState来禁用带反射的耳机。有没有其他方法可以做到这一点或更新以使这种方式工作?请帮忙!!

以下是我目前用于禁用耳机的代码:

  AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    try {
        Class<?> cl = Class.forName("android.media.AudioManager");
        setWiredDeviceConnectionState = 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
        setWiredDeviceConnectionState.invoke(manager, new Object[]{4, 0, "device"});
        setWiredDeviceConnectionState.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();
    }

当我在新手机上运行此代码时,这是个例外:

W/System: ClassLoader referenced unknown path: /data/app/com.example.raptorroboticstest-2/lib/arm64
W/System.err: java.lang.NoSuchMethodException: setWiredDeviceConnectionState [int, int, class java.lang.String]

0 个答案:

没有答案