Android获取通话状态

1>添加读取手机状态的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 


2>获取管理手机的系统服务

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

3>编写手机状态监听类


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); 
  } 
} 
  


4>注册监听


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












你可能感兴趣的:(java,android)