2010-12-01 19 views

Respuesta

16

usted tiene que utilizar el PhoneStateListener:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

// somewhere else 
private PhoneStateListener mPhoneListener = new PhoneStateListener() { 
    public void onCallStateChanged(int state, String incomingNumber) { 
     try { 
      switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       // do something... 
       break; 

      case TelephonyManager.CALL_STATE_OFFHOOK: 
       // do something... 
       break; 

      case TelephonyManager.CALL_STATE_IDLE: 
       // do something... 
       break; 
      default: 
       Log.d(TAG, "Unknown phone state=" + state); 
      } 
     } catch (RemoteException e) {} 
    } 
}; 

Asegúrese de incluir este permiso en su Manifiesto:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>