【问题标题】:How to make missed calls?如何拨打未接电话?
【发布时间】:2011-06-20 07:53:25
【问题描述】:

我正在开发一个我想成为的 Android 应用程序 能够拨打电话但有非常精确的限制,这是 “拨打未接电话”。我想要的是,能够挂断电话 电话响起的那一刻。

现在我可以知道手机何时开始尝试制作 呼叫,但在几秒钟内没有“振铃”活动在 网络,这是我愿意做的。

我怎样才能停止这一刻?

【问题讨论】:

    标签: android phone-call


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      PhoneStateListener中使用onCallStateChanged()

      【讨论】:

        猜你喜欢
        • 2018-11-20
        • 1970-01-01
        • 2016-02-26
        • 2014-12-20
        • 2017-12-21
        • 2019-05-16
        • 1970-01-01
        • 2017-08-26
        相关资源
        最近更新 更多