Broadcast recipient of Android four components

Source: Internet
Author: User

Describes how to create a broadcast, send an out-of-order broadcast and orderly broadcast, listen to text messages, and listen to outgoing calls (when we text and make a call, the system sends a broadcast that we can intercept to listen for text messages and listen for outgoing calls).

Define broadcast receivers

1. Defining class inheritance Broadcastreceiver, overriding the OnReceive method

2. The OnReceive method is executed when a matching broadcast is received

3. Declare <receiver> in the manifest file, where you need to configure <intent-filter> specify the actions and types of the receive broadcast

4.BroadcastReceiver, in addition to being declared in the manifest file, can also be declared in the code, using the Registerreceiver method to register receiver

Send broadcast

Shuffle broadcast

1. Use the Sendbroadcast method to send

2. Received by all broadcast receivers, unordered, non-disruptive

3. You can set recipient permissions when broadcasting, only if the recipient has permission to receive

4. Recipient's <receiver> can also set sender permissions, only receive broadcast with permission app

Orderly broadcast

1. Use the Sendorderedbroadcast method to send

2. Recipients can define android:priority in <intent-filter> to define priority, the higher the number the higher the priority

3. Received by each broadcast recipient, can interrupt or add data halfway

Abortbroadcast ()//Interrupt Broadcast

Getresultextras (True). Putstring ("Data", "new"); Add data

Getresultextras (True). GetString ("Data")//Receive data

Listen to SMS Receive

The 1.Android system sends an ordered broadcast when a message is received, and if we define a recipient to receive the broadcast, we can get the text message and intercept the SMS.

2. Define broadcast receivers to receive broadcast Android.provider.Telephony.SMS_RECEIVED

3. Call Intent's Getextras () inside the OnReceive method to get the PDUs field, and get a object[], where each element is a byte[]

4. Creating Smsmessage objects through the Createfrompdu method of the Smsmessage class

5. From the Smsmessage object, you can get the sender number, text message content, sending time and other information

6. Need to receive SMS permission: <uses-permission android:name= "Android.permission.RECEIVE_SMS"/>

7.Android system in the receipt of SMS notification is an ordered notification, we need to intercept spam messages, you can configure a higher priority, receive information to determine whether Abortbroadcast ()

Example:

Listing

  1. <? XML version="1.0" encoding="Utf-8" ?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. Package ="cn.test"
  4. Android:versioncode="1"
  5. android:versionname="1.0">
  6.      < application  android:icon = "@drawable/icon"  android:label = "@string/app_name" >   
  7. <activity android:name=". Mainactivity "
  8. Android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="Android.intent.action.MAIN" />
  11. <category android:name="Android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </Activity>
  14. <receiver android:name=". Smsreceiver ">
  15.              <intent-filter  android:priority = "999" >   
  16. <action android:name="Android.provider.Telephony.SMS_RECEIVED" />
  17. </intent-filter>
  18. </receiver>
  19. </Application>
  20. <uses-sdk android:minsdkversion="8" />
  21. <!--receive SMS rights --
  22. <uses-permission android:name="Android.permission.RECEIVE_SMS"/>
  23. </manifest>

Listen to SMS broadcasts:

  1. Package cn.test;
  2. import Java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import android.content.BroadcastReceiver;
  5. import Android.content.Context;
  6. import android.content.Intent;
  7. import android.telephony.SmsMessage;
  8. Public class smsreceiver extends broadcastreceiver {
  9. Public void onreceive (context context, Intent Intent) {
  10. Object[] PDUs = (object[]) Intent.getextras (). Get ("PDUs");
  11. For (Object pdu:pdus) {
  12. //Create a text message
  13. Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU);
  14. //Get the phone number to send
  15. String address = sms.getoriginatingaddress ();
  16. //Get the content of the SMS
  17. String BODY = Sms.getmessagebody ();
  18. //Time to get SMS
  19. String time = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date (  Sms.gettimestampmillis ()));
  20. System.out.println (time);
  21. SYSTEM.OUT.PRINTLN (address);
  22. System.out.println (body);
  23. }
  24. //Interrupt phone receive Operation
  25. Abortbroadcast ();
  26. }
  27. }

Listen to the outgoing call

1. Define the broadcast recipient to receive Android.intent.action.NEW_OUTGOING_CALL

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

3. Use the Getresultdata () and Setresultdata () methods in the OnReceive method to get and set the phone number

  1. Public void onreceive (context context, Intent Intent) {
  2. //Get to phone number
  3. String num = Getresultdata ();
  4. // ... Whether the query is local
  5. //operation of the acquired phone number
  6. Setresultdata ("17951" + num);
  7. }

Life cycle

1. The life cycle of the broadcast receiver is very short, created when the broadcast is received, and destroyed after the OnReceive () method finishes

2. Do not take some time-consuming work in the broadcast receiver, or the Application No Response error dialog box will pop up

3. It is best not to create sub-threads in the broadcast receiver to do time-consuming work, because the broadcast receiver is destroyed after the process becomes an empty process, it is easy to be killed by the system

4. Time-consuming longer work is best done in the service

Broadcast recipient of Android four components

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.