How to know Phone call has ended?

ⅰ亾dé卋堺 提交于 2021-02-08 03:59:54

问题


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:

回答1:


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;
}
}



回答2:


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




回答3:


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.




回答4:


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)


来源:https://stackoverflow.com/questions/20257222/how-to-know-phone-call-has-ended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!