Whether it is a call or call, the system broadcast will send the corresponding message, we can listen to the broadcast to get the phone status.
First, configure your own broadcastreceiver in the manifest file and declare the permissions.
<Cycler Android: Name = ". filter. phonestatreceiver "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. phone_state "/> <br/> <action Android: Name =" android. intent. action. new_outgoing_call "/> <br/> </intent-filter> <br/> </Cycler> </P> <p> <uses-Permission Android: Name =" android. permission. read_phone_state "> </uses-Permission> <br/> <uses-Permission Android: Name =" android. permission. process_outgoing_cils "> </uses-Permission>
Handle it in your own broadcastreceiver
<Br/> Import android. app. service; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. telephony. telephonymanager; <br/> Import android. util. log; </P> <p> public class phonestatreceiver extends broadcastreceiver {</P> <p> Private Static final string tag = "phonestatreceiver "; </P> <p> Private Static Boolean incomingflag = false; </P> <p> Private Static string incoming_number = NULL; </P> <p> @ override <br/> Public void onreceive (context, intent) {<br/> // if you are calling <br/> If (intent. getaction (). equals (intent. action_new_outgoing_call) {<br/> incomingflag = false; <br/> string phonenumber = intent. getstringextra (intent. extra_phone_number); <br/> log. I (TAG, "Call out:" + phonenumber ); <br/>} else {<br/> // If the caller is calling <br/> telephonymanager TM = <br/> (telephonymanager) context. getsystemservice (service. telephony_service); </P> <p> switch (TM. getcallstate () {<br/> case telephonymanager. call_state_ringing: <br/> incomingflag = true; // identifies the current incoming call. <br/> incoming_number = intent. getstringextra ("incoming_number"); <br/> log. I (TAG, "ringing:" + incoming_number); <br/> break; <br/> case telephonymanager. call_state_offhook: // there must be at least one call dialing, active, or // retained, and no call ringing or waiting <br/> If (incomingflag) {<br/> log. I (TAG, "Incoming accept:" + incoming_number); <br/>}< br/> break; </P> <p> case telephonymanager. call_state_idle: // idle status <br/> If (incomingflag) {<br/> log. I (TAG, "Incoming idle"); <br/>}< br/> break; <br/>}< br/>}