问题
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