如何制造未接来电?

10

我正在开发一个 Android 应用,希望能够进行通话,但有一个非常特殊的限制,即“漏接电话”。我的要求是,在手机开始响铃时就能挂断。

目前,我能够知道手机何时开始尝试拨打电话,但在接下来的几秒钟内,网络上没有“响铃”活动,这正是我想要的。

如何精确地停止电话呼叫?

2个回答

2
使用通过PhoneStateListener的onCallStateChanged()方法,您只能检测到电话开始拨出和通话结束时的情况,但无法确定何时开始响铃。我曾尝试过,请查看下面的代码:
一个外拨电话从IDLE状态开始到OFFHOOK状态(拨号),然后在挂断时回到IDLE状态。 唯一的解决办法是使用计时器,在外拨电话开始并经过数秒钟后挂断,但这样仍无法保证手机会开始响铃。
以下是代码:
  public abstract class PhoneCallReceiver extends BroadcastReceiver {
        static CallStartEndDetector listener;

  

    @Override
    public void onReceive(Context context, Intent intent) {
        savedContext = context;
        if(listener == null){
            listener = new CallStartEndDetector();
        }


            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
        telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

   

    public class CallStartEndDetector extends PhoneStateListener {
        int lastState = TelephonyManager.CALL_STATE_IDLE;
        boolean isIncoming;

        public PhonecallStartEndDetector() {}


        //Incoming call-   IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
        //Outgoing call-  from IDLE to OFFHOOK when dialed out, to IDLE when hunged up

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            if(lastState == state){
                //No change
                return;
            }
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    isIncoming = true;
                     //incoming call started
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //Transition of ringing->offhook are pickups of incoming calls.  Nothing down on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                        isIncoming = false;
                       //outgoing call started
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    //End of call(Idle).  The type depends on the previous state(s)
                    if(lastState == TelephonyManager.CALL_STATE_RINGING){
                        // missed call
                    }
                    else if(isIncoming){
                          //incoming call ended
                    }
                    else{
                       //outgoing call ended                                              
                    }
                    break;
            }
            lastState = state;
        }

    }



}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接