开发者

How to make missed calls?

I am working on an Android app on which I want to be able to make calls but with a very precise restriction, this is "making missed calls". What I want is, to be able to hang up just the moment the phone starts ringing.

right now I am able to know when the phone starts to try and make the call, but for a few seconds there is no "ringing" activit开发者_如何学运维y over the network, which is what I am willing to do.

How can I stop this exact moment?


Using the onCallStateChanged() via the PhoneStateListener you can only detect when the phone starts to make an outgoing call and when the outgoing call is hunged up but you can't determine when a "ringing" is started. I tried once, check out the code below:

An outgoing call starts from IDLE to OFFHOOK when dialed out, to IDLE when hunged up. The only workaround is to use a timer to hang up after the outgoing call starts and some few seconds passes, but then, you're never guaranteed the phone will start ringing.

Here's the code:

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

    }



}


Use onCallStateChanged() in PhoneStateListener

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜