0

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 .

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

1 Answers1

1

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.

Yonatan Dawit
  • 589
  • 7
  • 11
  • your answer is helpful but it is giving me data of whether it is calling or connected means , it means i can not get if phone was answered or not . i want to record time from call receiving to call end – android_developer Sep 11 '20 at 12:43
  • You asked for the code when the mic is open, as I said whenever to initiate a telephony service, the mic will be open not when the person answers. Maybe you can find a broadcast when the call is answered. Use this reference https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected%23:~:text%3DAnd%2520youre%2520done%252C%2520just%2520run,toast%2520when%2520call%2520is%2520answered.&ved=2ahUKEwjVhunml-PrAhWByYUKHeMGDcAQFjABegQIDBAF&usg=AOvVaw3fCWcqUnoYvs7jvXy6Utbj – Yonatan Dawit Sep 12 '20 at 08:21
  • 1
    well , you are right i had asked for whether mic is opened or not . your answer works well according answer , i have accepted your answer – android_developer Sep 14 '20 at 04:15