这就是我使用的......onCallStateChanged()
只要呼叫状态发生变化,就会在所有 PhoneStateListeners 中调用该函数,所以这就是您需要处理的。
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
//Makes sure the audio is paused for incoming/outgoing phone calls
public class ListenToPhoneState extends PhoneStateListener {
private boolean pausedForPhoneCall = false;
private UIManager uiManager;
ListenToPhoneState(UIManager manager){
uiManager = manager;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
resumeInAndroid();
return;
case TelephonyManager.CALL_STATE_OFFHOOK:
pauseInAndroid();
return;
case TelephonyManager.CALL_STATE_RINGING:
pauseInAndroid();
return;
}
}
private void resumeInAndroid(){
if(pausedForPhoneCall == true) {
pausedForPhoneCall=false;
uiManager.waitForPhoneCall(false);
}
}
private void pauseInAndroid(){
if(pausedForPhoneCall == false){
pausedForPhoneCall=true;
uiManager.waitForPhoneCall(true);
}
}
String stateName(int state) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: return "Idle";
case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
}
return Integer.toString(state);
}
}
如果您想将此添加到您当前的活动中,请执行此操作..
ListenToPhoneState listener = new ListenToPhoneState(userInterface);
TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if(tManager != null)
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
您不想暂停当前的活动吗?如果你向你的类传递一个当前活动的实例,你可以调用 finish() 并在 switch 语句中创建一个新活动,如果你真的想要的话。