Android obtains the phone status and listens to the phone call status, and android listens
Get Mobile Phone status:
Import android. content. context; import android. telephony. telephonyManager; // obtain the corresponding system service TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE);/*** return call status ** when CALL_STATE_IDLE is not in any status * When CALL_STATE_OFFHOOK receives the call * When CALL_STATE_RINGING comes in */tm. getCallState (); if (tm. getCallState () = TelephonyManager. CALL_STATE_IDLE) {Log. d ("test", "call state idle... ");} else if (tm. getCallState () = TelephonyManager. CALL_STATE_OFFHOOK) {Log. d ("test", "call state offhook... ");} else if (tm. getCallState () = TelephonyManager. CALL_STATE_RINGING) {Log. d ("test", "call state ringing... ");}
Listen to the phone call status:
// Obtain the corresponding system service TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); // use the listen (PhoneStateListener listener, int events) of the TelephonyManager object) // implement the PhoneStateListener listener and implement the corresponding method public class MyPhoneCallListener extends PhoneStateListener {@ Override public void onCallStateChanged (int state, String incomingNumber) {switch (state) {case TelephonyManager. CALL_STATE_OFFHOOK: // call status Toast. makeText (Main. this, "talking... ", Toast. LENGTH_SHORT ). show (); break; case TelephonyManager. CALL_STATE_RINGING: // call status Toast. makeText (Main. this, incomingNumber, Toast. LENGTH_SHORT ). show (); break;} super. onCallStateChanged (state, incomingNumber );}
The first parameter needs to implement the PhoneStateListener listener and implement the corresponding method. The second parameter is the static constant of PhoneStateListener. In this case, because it is a listener, you need to input LISTEN_CALL_STATE, at the same time, you also need to register the corresponding permissions in AndroidManifest
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>