The basic idea of listening to the phone status on Android is to register and accept the broadcast of the phone status. The implementation is as follows:
First, you need the following permissions to listen to the phone status:
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<Uses-Permission Android: Name = "android. Permission. process_outgoing_cils"/>
Second, accept the broadcast of phone status:
Public class phonereceiver extends broadcastreceiver {Private Static final string tag = "phonereceiver"; Public void onreceive (context, intent) {log. I (TAG, "onreceive ()"); string action = intent. getaction (); If (action. equals (intent. action_new_outgoing_call) {// call String outnumber = This. getresultdata (); // call number} else if ("android. intent. action. phone_state ". equals (Action) {// call String state = intent. getstringextra (telephonymanager. extra_state); string innumber = intent. getstringextra (telephonymanager. extra_incoming_number); // call number if (state. equalsignorecase (telephonymanager. extra_state_ringing) {// The phone is ringing} else if (state. equalsignorecase (telephonymanager. extra_state_idle) {// hang up} else if (state. equalsignorecase (telephonymanager. extra_state_offhook) {// disconnect, call status }}}}
Finally, in androidmanifest. XML, configure the handler:
<receiver android:name=".PhoneReceiver" > <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>