Android 监听电话状态

 TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 mTelephonyManager.listen(new PhoneCallListener(), PhoneCallListener.LISTEN_CALL_STATE);
 
 public class PhoneCallListener extends PhoneStateListener
 {
  @Override
  public void onCallStateChanged(int state, String incomingNumber)
  {
   switch (state)
   {
    case TelephonyManager.CALL_STATE_IDLE:
     Log.v(this.getClass().getName(), "onCallStateChanged-state: " + state);
    break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
     Log.v(this.getClass().getName(), "onCallStateChanged-state: " + state);
    break;
    case TelephonyManager.CALL_STATE_RINGING:
     Log.v(this.getClass().getName(), "onCallStateChanged-incomingNumber: " + incomingNumber);
    break; 
   }
   super.onCallStateChanged(state, incomingNumber);
  }
 }

权限设置:

 

你可能感兴趣的:(Andorid)