The realization method of the Android monitor incoming call and power off _android

Source: Internet
Author: User
Tags sqlite database

The example of this article is about the implementation of the Android monitor caller and power off. Share to everyone for your reference, specific as follows:

To monitor the Android phone and answer the phone, just the following 2 steps

The first step is to write a receiver inherited from Broadcastreceiver

Import Android.app.Service;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.TelephonyManager;
Import Android.util.Log; public class Phonestatebroadcastreceiver extends Broadcastreceiver {private static final String TAG = "PHONESTATEBROADCA
 Streceiver ";
 private static Boolean mincomingflag = false;
 private static String mincomingnumber = null; @Override public void OnReceive (context context, Intent Intent) {//If the call is (Intent.getaction (). Equals (Intent.acti
  On_new_outgoing_call)) {Mincomingflag = false;
  String PhoneNumber = Intent.getstringextra (Intent.extra_phone_number);
 LOG.I (TAG, "call out:" + PhoneNumber); else {//if the caller telephonymanager Tmanager = (telephonymanager) context.getsystemservice (service.telephony_service
  ); Switch (tmanager.getcallstate ()) {Case TelephonyManager.CALL_STATE_RINGING:mIncomingNumber = Intent.getstringextra
  ("Incoming_number"); LOG.I (TAG, "ringing:" + mincomingnumber);
  Break
  Case TelephonyManager.CALL_STATE_OFFHOOK:if (Mincomingflag) {log.i (TAG, "Incoming ACCEPT:" + mincomingnumber);
  } break;
  Case TelephonyManager.CALL_STATE_IDLE:if (Mincomingflag) {log.i (TAG, "incoming IDLE");
  } break;

 }
 }
 }
}

Step two: In Androidmanifest.xml, configure the written receiver, and intercept the corresponding broadcastaction,

Also note that the appropriate permissions are added

<receiver android:name= ". Broadcase. Phonestatebroadcastreceiver ">
   <intent-filter>
    <action android:name=" Android.intent.action.PHONE_STATE "/>
    <action android:name=" Android.intent.action.NEW_OUTGOING_CALL " >
   </intent-filter>
 </receiver>
<uses-permission android:name= " Android.permission.READ_PHONE_STATE "/>
<uses-permission android:name=" android.permission.PROCESS_ Outgoing_calls "/>

PS: Here again to provide you with a Androidmanifest.xml access control online query tool:

Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest

More interested readers of Android content can view this site: "Android SMS and phone Operation tips Summary", "Android operation SQLite Database Skills Summary", "Android Development introduction and Advanced Course", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.