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

女生的网名这么多〃 提交于 2019-12-20 05:38:29

问题


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.

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. I figure I must be doing something stupid, or missing something else that I can check?


回答1:


You are correct in that you don't get any state updates when a out going call is started.

You need to hook into the intent broadcast ACTION_NEW_OUTGOING_CALL. Here is a Android Developer Blog that may help understand the fact that it's a ordered broadcast. And here is a blog post with a simple example of how to setup and use this broadcast.



来源:https://stackoverflow.com/questions/6673368/android-2-1-pause-for-incoming-outgoing-call-resume-when-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!