Telephonymanager mtelephonymgr = (telephonymanager) getsystemservice (context. telephony_service );
Telephonymanager provides a method for obtaining mobile phone information, including call status, network connection, and carrier information,
The following example shows how telephonymanager monitors the call status:
import android.os.Bundle;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context; import android.content.IntentFilter;import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { private static final String TAG = "Telephony"; TextView view = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TelephonyManager mTelephonyMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE); view = new TextView(this); view.setText("listen the state of phone\n"); setContentView(view); } class TeleListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: { Log.d(TAG, "CALL_STATE_IDLE"); view.append("CALL_STATE_IDLE " + "\n"); break; } case TelephonyManager.CALL_STATE_OFFHOOK: { Log.d(TAG, "CALL_STATE_OFFHOOK"); view.append("CALL_STATE_OFFHOOK" + "\n"); break; } case TelephonyManager.CALL_STATE_RINGING: { Log.d(TAG, "CALL_STATE_RINGING"); view.append("CALL_STATE_RINGING" + "\n"); break; } default: Log.d(TAG, " the statu is"+state); break; } } } }