如何拨打未接来电?

发布于 2024-11-16 08:41:03 字数 164 浏览 4 评论 0原文

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

现在我能够知道手机何时开始尝试并拨打电话 呼叫,但几秒钟内没有“响铃”活动 网络,这是我愿意做的。

我怎样才能停止这一刻呢?

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" activity over the
network, which is what I am willing to do.

How can I stop this exact moment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-11-23 08:41:03

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

    }



}

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

    }



}
墨离汐 2024-11-23 08:41:03

PhoneStateListener 中使用 onCallStateChanged()

Use onCallStateChanged() in PhoneStateListener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文