如何检查安卓设备上麦克风是否已打开?

huangapple go评论93阅读模式
英文:

how to check if mic is turned on or not in android?

问题

我正在尝试开发电话通话跟踪功能,即了解用户在我的应用上进行电话通话的时间。我了解有关通话的呼入和呼出广播。但对于呼出电话,有可能对方不会接听或网络不可达。我在网上阅读了一些解决方案,但我认为当有人接听电话时,麦克风应该在那时候打开。因此,如果我能够获取麦克风打开的广播,我的程序将会完成。我尝试过但没有发现任何信息,如果有人知道,请帮忙。

提前感谢您。

英文:

i am trying to develeope track of phone call , means how much user spends time on phone call from my app . i know about incoming and outgoing broadcast for call . but for outgoing call there are chances that person will not answer or network unreachable . i read some solution online but i thought when someone answers the call , mic should be turn on at that time . so if somehow i can get broadcast of mic turning on . my program would be complete , i tried but found nothing , if anybody knows please help .

Thank You in advance .

答案1

得分: 1

我认为一旦你使用电话服务发起通话,无论对方是否接听你的通话,麦克风都会打开,但如果你真的想要的话,你可以像这样使用AudioManager服务。

AudioManager audioManager = (AudioManager) getSystemService(context);

// 这是获取AudioManager的活动模式
int mode = audioManager.getMode();

// 对于语音通话,请使用AudioManager.MODE_IN_CALL
// 对于音视频聊天或VOIP,请使用AudioManager.MODE_IN_COMMUNICATION

if (AudioManager.MODE_IN_CALL == mode) {
   // 在通话进行时进入此处。
}

以下是完整的方法。

public void listener(){
    final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

    new Thread(new Runnable() {
        @Override
        public void run() {
            while(true){
                // 这是获取AudioManager的活动模式
                int mode = audioManager.getMode();

                // 对于音频通话,请使用AudioManager.MODE_IN_CALL
                // 对于音视频聊天或VOIP,请使用AudioManager.MODE_IN_COMMUNICATION

                if (AudioManager.MODE_IN_COMMUNICATION == mode) {
                    // 在活动的互联网通话中进入此处。
                    Log.e(TAG, "在互联网通话中(VOIP)");
                } else if (AudioManager.MODE_IN_CALL == mode) {
                    // 在活动的通话中进入此处。
                    Log.e(TAG, "在普通通话中");
                }

                try {
                   Thread.sleep(1000);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
            }
        }
    }).start();
}

现在只需在你的类中调用 listener() 方法。

开发者文档中阅读更多信息

英文:

I think once you start a call using Telephony service, the microphone will open regardless if the other side picks up your call or not, but if you really want you can use the AudioManager service like so.

AudioManager audioManager = (AudioManager) getSystemService(context);

// This is to get the active mode of the AudioManager
int mode = audioManager.getMode();

// use AudioManager.MODE_IN_CALL for audio call
// use AudioManager.MODE_IN_COMMUNICATION for audio/video chat or VOIP 

if (AudioManager.MODE_IN_CALL == mode) {
   // Enters here during active call.
}

Here is the full method.

    public void listener(){
        final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    // This is to get the active mode of the AudioManager
                    int mode = audioManager.getMode();

                    // use AudioManager.MODE_IN_CALL for audio call
                    // use AudioManager.MODE_IN_COMMUNICATION for audio/video chat or VOIP

                    if (AudioManager.MODE_IN_COMMUNICATION == mode) {
                        // Enters here during active internet call.
                        Log.e(TAG, "In Internet Call (VOIP)");
                    }else if (AudioManager.MODE_IN_CALL == mode) {
                        // Enters here during active call.
                        Log.e(TAG, "In Normal Call");
                    }

                    try {
                       Thread.sleep(1000);
                    } catch (InterruptedException e) {
                       e.printStackTrace();
                    }
                }
            }
        }).start();
    }

Now just call the listener() method in your Class.

Read more about it Here on the developer documentation.

huangapple
  • 本文由 发表于 2020年9月11日 16:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63843083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定