0

我正在创建一个应用程序,只需要捕获来电。我为此使用 BroadCast 类和 Phone Listener 类,并在 PhoneListnere 类中捕获是否有来电。我的问题是,如果有任何拨出电话,我不希望我的应用程序被触发。即使有任何拨出电话,我的广播类也会被触发(尽管我正在为拨出电话巧妙地退出应用程序)但不喜欢应用程序实际上被触发的想法。是否有任何特定的意图来处理来电?

4

1 回答 1

0

您可以使用ACTION_PHONE_STATE_CHANGED意图,并存储最后一个状态。如果当前状态为TelephonyManager.EXTRA_STATE_OFFHOOK,则存在正在拨号、活动或保持的呼叫。如果之前的状态是 RINGING,那么这是一个来电。如果之前的状态是 IDLE,那么这是一个拨出呼叫。

以下接收器区分不同的场景:

public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
        // Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    }else if(state.equals(TelephonyManager.EXTRA_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.
        if(lastState.equals(TelephonyManager.EXTRA_STATE_IDLE){
            // outgoing call                
        } else if(lastState.equals(TelephonyManager.EXTRA_STATE_RINGING){
            // incoming call
        }
    }else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
        // Device call state: No activity.
    }
    lastState = state;
}
于 2013-10-28T12:54:00.610 回答