简体   繁体   English

Android 2.1+ 暂停呼入/呼出,完成后恢复

[英]Android 2.1+ Pause for incoming/outgoing call, resume when done

Is there a reliable way in Android to detect the beginning and end of incoming and outgoing calls, in order to do things like pause and play audio. Android 中是否有可靠的方法来检测来电和去电的开始和结束,以便执行暂停和播放音频等操作。

I have done the following:我做了以下事情:

    telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            callEventListener = new PhoneStateListener(){

                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    super.onCallStateChanged(state, incomingNumber);
                    if (state == TelephonyManager.CALL_STATE_IDLE){
                        //....
                    }
                    else if (state == TelephonyManager.CALL_STATE_RINGING){
                        //....
                    }
                }
            };
telephonyManager.listen(callEventListener, PhoneStateListener.LISTEN_CALL_STATE);

Which seems to work for incoming calls, but for outgoing calls I get the CALL_STATE_IDLE when the other person picks up...not when the phone call ends.这似乎适用于来电,但对于去电,我会在对方接听时收到 CALL_STATE_IDLE ......而不是在电话结束时。 I figure I must be doing something stupid, or missing something else that I can check?我想我一定是在做一些愚蠢的事情,或者错过了我可以检查的其他事情?

You are correct in that you don't get any state updates when a out going call is started.您是正确的,因为在开始外拨电话时您没有收到任何 state 更新。

You need to hook into the intent broadcast ACTION_NEW_OUTGOING_CALL .您需要连接到意图广播ACTION_NEW_OUTGOING_CALL Here is a Android Developer Blog that may help understand the fact that it's a ordered broadcast.这是一个Android 开发者博客,它可能有助于理解它是一个有序广播的事实。 And here is a blog post with a simple example of how to setup and use this broadcast.这是一篇博客文章,其中包含如何设置和使用此广播的简单示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM