Android获取通话状态

[size=medium]1>添加读取手机状态的权限 [/size]




[size=medium]2>获取管理手机的系统服务[/size]


TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

[size=medium]3>编写手机状态监听类 [/size]



public class exPhoneCallListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE ://待机
break;
case TelephonyManager.CALL_STATE_OFFHOOK ://挂断
break;
case TelephonyManager.CALL_STATE_RINGING ://来电话
//来电免打扰程序要处理的关键事务
break;
default :
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}


[size=medium]4>注册监听 [/size]



exPhoneCallListener myPhoneCallListener = new exPhoneCallListener();
tm.listen(myPhoneCallListener,PhoneStateListener.LISTEN_CALL_STATE);

你可能感兴趣的:(Android)