思路:就是一個監聽,很簡單
代碼如下你自己試試:
private class EndCallListener extends PhoneStateListener {
boolean flag =false ;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
//鈴聲狀态
AudioManager aui =(AudioManager) CallActivity.this.getSystemService(Context.AUDIO_SERVICE);
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE://閑置或結束電話
aui.setMode(AudioManager.RINGER_MODE_NORMAL);
if(flag){
flag= false ;
Toast.makeText(CallActivity.this, "通話結束", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(CallActivity.this, "閑置", Toast.LENGTH_LONG).show();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK://通話中
flag= true ;
Toast.makeText(CallActivity.this, "通話中", Toast.LENGTH_LONG).show();
break ;
case TelephonyManager.CALL_STATE_RINGING://來電
Toast.makeText(CallActivity.this, "來電", Toast.LENGTH_LONG).show();
call_incom(incomingNumber,aui);
}
}
在activity的onCreate中:
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)CallActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);