Search code examples
javaandroidbroadcastreceiverarray-broadcasting

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 .


Solution

  • 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.