如何拨打未接来电?

Ara*_*cem 10 android phone-call

我正在开发一个Android应用程序,我希望能够拨打电话但是有一个非常精确的限制,这就是"拨打未接来电".我想要的是,只要电话开始振铃就能挂断电话.

现在我能够知道电话何时开始尝试拨打电话,但是几秒钟内网络上没有"振铃"活动,这是我愿意做的.

我怎么能停止这个确切的时刻?

Nan*_*tey 3

通过 PhoneStateListener使用,onCallStateChanged()您只能检测手机何时开始拨出电话以及何时挂断拨出电话,但无法确定何时开始“响铃”。我尝试过一次,请查看下面的代码:

拨出时,拨出呼叫从 开始IDLEOFFHOOK挂机时从 到 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;
        }

    }



}
Run Code Online (Sandbox Code Playgroud)