Question

What is current Phone state at the time of call end.

In android there are three states.

TelephonyManager.CALL_STATE_IDLE
TelephonyManager.CALL_STATE_OFFHOOK
TelephonyManager.CALL_STATE_RINGING:
Was it helpful?

Solution

This code works for me. Hope it will help you

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CallStateReceiver extends BroadcastReceiver {

private final String LOG_TAG = "CallStateReceiver";

public static String prevState = TelephonyManager.EXTRA_STATE_IDLE;

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    if (bundle == null) {
        return;
    }

    String state = bundle.getString(TelephonyManager.EXTRA_STATE);

    if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)
            && !prevState.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {

        Log.i(this.LOG_TAG, "Call ended"));
    }

    prevState = state;
}
}

OTHER TIPS

TelephonyManager.CALL_STATE_IDLE    #When a call end, no matter what, call received by user or not

TelephonyManager.CALL_STATE_OFFHOOK #when user receive a call

TelephonyManager.CALL_STATE_RINGING #when phone ring (incoming call)

Once the call has ended the phone state should be TelephonyManager.CALL_STATE_IDLE

Use PhoneStateListener to to receive notification of changes in specified telephony states.

Overide onCallStateChanged (int state, String incomingNumber) and check the state parameter

if (state == CALL_STATE_IDLE) {  
    //this is current Phone state at the time of call end, handle call end here
}

The three states indicate the following:

CALL_STATE_IDLE -> Device call state: No activity.
CALL_STATE_OFFHOOK -> Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
CALL_STATE_RINGING -> Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.

For more information, please refer the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top