【问题标题】:How to detect hangup of an incoming call which was not answered.?如何检测未接听的来电挂断。?
【发布时间】:2026-01-23 19:50:02
【问题描述】:

我发现了许多问题,这些问题描述了如何使用电话管理器获取电话的通话状态。我在 * 中关注了这个问题 - How to detect when phone is answered or rejected。但这是为了分别检测各种手机状态。 只有在来电挂断且无人接听时,我才需要添加提醒对话框。 如何检测此特定事件。?

【问题讨论】:

  • 我认为该链接中的答案非常适合您的情况。听起来您想检测拒绝来电

标签: android broadcastreceiver call telephonymanager


【解决方案1】:

Android 5.0 及更高版本可能会发送重复事件。您可以考虑保留之前的状态进行比较 -

private class PhoneStateChangeListener extends PhoneStateListener {

static final int IDLE = 0;
static final int OFFHOOK = 1;
static final int RINGING = 2;
int lastState = IDLE;

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
             lastState = RINGING;
             break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
             lastState = OFFHOOK;
             break;
        case TelephonyManager.CALL_STATE_IDLE:
             if (lastState == RINGING) {
                 // Process for call hangup without being answered
             }
             lastState = IDLE;
             break;
    }
}
}

【讨论】:

【解决方案2】:

试试这个,

你应该像这样创建广播接收器,

public class CallReciever extends BroadcastReceiver {

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

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

                // Phone number 
                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

                // Ringing state
                // This code will execute when the phone has an incoming call
        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            // This code will execute when the call is answered or disconnected
        }

    }
}

还在您的 manifest.xml 文件中添加以下内容。

<receiver android:name=".CallReciever" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
 </receiver>

【讨论】:

    【解决方案3】:

    试试下面的代码:

    private class PhoneStateChangeListener extends PhoneStateListener {
    boolean isAnswered = false;
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                 isAnswered  = false;
                 break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 isAnswered=true;
                 break;
            case TelephonyManager.CALL_STATE_IDLE:
                 if(isAnswered == false)
                 //Call hangup without answered
                 break;
        }
      }
    }
    

    【讨论】:

    • 感谢您的快速回复。我还有一个疑问,即如何在上述代码中添加警报对话框。这是必需的上下文参数。当我尝试使用它时发生致命错误。