有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    我找到了适用于我的解决方案here。并添加了以下代码:

    private static boolean headsetMic;
    
    private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
                final int headphones = intent.getIntExtra("state", -1);
                final int mic = intent.getIntExtra("microphone", -1);
                // we need to check both of them, because if headset with
                // mic was disconnected mic is 1 but headphones is 0, and
                // actually no external mic is connected
                headsetMic = headphones > 0 && mic > 0;
            }
        }
    };
    
    public static boolean isExternalMicConnected() {
        return headsetMic;
    }
    

    我调用isExternalMicConnected(),它会显示自定义麦克风是否已连接