Android-Get the phone status

Source: Internet
Author: User

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/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.