How to get the call status for outgoing calls?

11,904

Solution 1

Below code help you to get net work status code in onCreate

     StateListener phoneStateListener = new StateListener();
     TelephonyManager telephonyManager =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
     telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

create an inner class for listening state of phone.

class StateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            System.out.println("call Activity off hook");
            LockScreenActivity.this.finish();
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            break;
    }
}
};

Set permission in manifest file

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Solution 2

BroadcastReceiver with action for outgoing call android.intent.action.NEW_OUTGOING_CALL and android.intent.action.PHONE_STATE.

The android.intent.action.NEW_OUTGOING_CALL will be broadcasted when an outgoing call is initiated. The receiving intent will have an extra string variable Intent.EXTRA_PHONE_NUMBER which contains the outgoing number. This requires the permission android.permission.PROCESS_OUTGOING_CALLS.

To detect the incoming call, register a BroadcastReceiver for the action android.intent.action.PHONE_STATE. This will be broadcasted when there is a change in phone state. The receiving intent will have an extra string variable TelephonyManager.EXTRA_STATE which describes the phone state. If this state is TelephonyManager.EXTRA_STATE_RINGING then there will be another extra string variable TelephonyManager.EXTRA_INCOMING_NUMBER. This variable contains the incoming phone number. Note that this variable will not be present when the state is not TelephonyManager.EXTRA_STATE_RINGING.

Share:
11,904
Sachidananda Naik
Author by

Sachidananda Naik

Voracious Reader!

Updated on June 25, 2022

Comments

  • Sachidananda Naik
    Sachidananda Naik almost 2 years

    I am beginner in android and developing an android application to monitor the network status when the user is on call(for both incoming and outgoing calls)...

    i can monitor the network status .for incoming calls by implementing the PhonestateListner class and overriding the onCallStateChanged method, i am using the constants (CALL_STATE_IDLE,CALL_STATE_OFFHOOK and CALL_STATE_RINGING) defined in the TelephonyManager class inside the onCallStateChanged method , but these constants are not working properly for outgoing calls...

     my question is
    
    1. How to monitor the call status(RINGING,OFF_HOOK, IDLE ) when there is an outgoing call??
    2. Is there any API in android to monitor the call status for outgoing calls??? if yes, then please specify...

    please help me to understand this concept... Thanks for your time

  • brasofilo
    brasofilo almost 10 years