這是我使用的...... 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語句中創建一個新的活動,如果您真的想要的話。
我看到那篇文章。我在哪裏添加該代碼?我不明白在哪裏添加它以及PhoneStateReciever如何被調用。 – AndroidDev93