android – 如何使错过的电话?

前端之家收集整理的这篇文章主要介绍了android – 如何使错过的电话?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个我想要的 Android应用程序
能够打电话但是有一个非常精确的限制,这是
“打错了电话”.我想要的是,只能挂断电话
一会儿电话开始响了.

现在我可以知道手机何时开始尝试制作
打电话,但几秒钟内没有“响”的活动
网络,这是我愿意做的.

我该如何阻止这个确切的时刻?

解决方法

通过PhoneStateListener使用onCallStateChanged(),您只能检测手机什么时候开始拨出电话,并且拨出电话被挂起,但是您无法确定何时启动“振铃”.我试过一下,看看下面的代码

拨打电话时,拨出的电话从空闲状态开始到OFFHOOK,挂断时从空闲状态开始.
唯一的解决方法是在拨出呼叫开始之后使用定时器挂断几秒钟,但是从来没有保证手机将开始响铃.

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

    }



}

猜你在找的Android相关文章