Android 2.1+ 来电/去电暂停,完成后恢复 [英] Android 2.1+ Pause for incoming/outgoing call, resume when done

查看:23
本文介绍了Android 2.1+ 来电/去电暂停,完成后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 中是否有一种可靠的方法来检测来电和去电的开始和结束,以便执行诸如暂停和播放音频之类的操作.

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.

我做了以下事情:

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

这似乎适用于来电,但对于拨出电话,当其他人接听时我得到 CALL_STATE_IDLE ......而不是在电话结束时.我想我一定是在做一些愚蠢的事情,还是遗漏了一些我可以检查的东西?

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?

推荐答案

您是对的,因为在开始外出呼叫时您没有收到任何状态更新.

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

您需要连接到意图广播 ACTION_NEW_OUTGOING_CALL.这是一个 Android 开发者博客,可能有助于理解事实上,这是一个有序的广播.这是一篇博客文章,其中包含一个关于如何设置和使用此广播的简单示例.

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.

这篇关于Android 2.1+ 来电/去电暂停,完成后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆