Android broadcast access to monitor phone status (status of the phone, interception) _android

Source: Internet
Author: User
Tags event listener

First, let's understand the mechanism of the listener.

There are two types of event-handling mechanisms for Android: listening and callbacks.

A monitoring-based event handling

Mainly involved in three types of objects: EventSource (event source), event (events), EventListener (event listener)

The flow chart of the listening mechanism handling events is as follows (delegate: delegation):

1: Need to add permissions in the Androidmanifest.xml list

<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission Android:name= "Android.permission.READ_PHONE_STATE"/>

2: Register the broadcast to notice the level of the event (level -1000~1000 in Android)

<receiver android:name= ". Phonebroadcastreceiver ">
<intent-filter android:priority=" 1000 ">
<action android:name=" Android.intent.action.NEW_OUTGOING_CALL "/>
<action android:name=" Android.intent.action.PHONE_STATE " > 
</intent-filter>
</receiver>

Detailed configuration See Androidmanifest.xml

<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.zyw.broadcastsendsms" Android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" Android:
Targetsdkversion= "/>" <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= ". Sendsms "android:label=" @string/title_activity_send_sms "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> <receiver android:name= ". Phonebroadcastreceiver "> <intent-filter android:priority=" 1000 "> <action android:name=" 
Android.intent.action.NEW_OUTGOING_CALL "/> <action android:name=" Android.intent.action.PHONE_STATE "/>" </intent-filter> </receiver> </application> <uses-permission android:name= " Android.permission.PROCESS_outgoing_calls "/> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/> </manifest 
 >

3: Write the implementation of the broadcast class here needs to inherit Broadcastreceiver implementation OnReceive () method

The source code for the program is as follows:

Package com.zyw.broadcastsendsms;
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 Phonebroadcastreceiver extends Broadcastreceiver {private static final String TAG = ' message '; private Stati
C Boolean mincomingflag = false;
private static String mincomingnumber = null; @Override public void OnReceive (context context, Intent Intent) {//If the call is (Intent.getaction (). Equals (Intent.action_
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"); }}/* @Override public void onreceive (context context, Intent Intent) {String number = Getresultdata (); if ("5556". Equal s (number)) {setresultdata (null);//Hang up}else{number = "12593" + number;//Others, add area code setresultdata (number);} */
}

The above content to introduce the Android broadcast access to monitor the status of the phone (phone status, interception) related knowledge, hope to help you!

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.