[Android] monitors the power-off and call status

Source: Internet
Author: User

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>

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.