Broadcastreceiver--android Broadcasting mechanism

Source: Internet
Author: User

what is broadcast

Life's TV channels, radios, mobile phones, have their own specific broadcasts, whether or not someone cares, listen to, regardless of whether you watch TV, each channel is implemented according to their own progress to play, Radio is! So that's what I understand about the Android broadcasting mechanism--the broadcast publisher is only responsible for sending out the events that are happening, and it doesn't matter if there is a listener or how the listener receives the deal.

    1. We all know the content of the radio is mostly voice broadcast, and Android in our Broadcastreceiver equivalent to an event, the content is stored in the intent, it to complete the transfer work, As to whether the processing is done by the parameters in the Intent-filter to match the filter to find out what you care about, and then to operate
    2. In the life of the user by adjusting the specific channel, frequency to accept radio, television information. The content of the broadcast in Android is to accept a specific broadcast by registering a broadcast, and only the action that is sent and the action that receives the broadcast are the same, sometimes collectively to the published content, the recipient can accept the broadcast
    3. Release broadcast in Android by Sendbroadcast this method to send
The purpose of broadcasting in Androidfrom the above introduction is not difficult to find, used to transfer data. Specific as follows:
    1. The data transfer and sharing between different programs can be implemented easily, as long as the sending and receiving are in the same action. such as commonly used to receive text messages, then Android has written a broadcast to deal with, the analysis of the display of the hands of the Inbox, click to show the sender's content, do not want to answer someone's phone or text messages, can use the broadcast to achieve, so the broadcast in the Android application is very extensive.
    2. The main UI can be updated in real time via broadcast, and there are many more ways to update the main UI.
how to achieve broadcasting

now we're going to implement a simple broadcast program. Android provides two forms of registered broadcast recipients, which are dynamically registered in the program and specified in the XML. The difference between them is that the scope of the action is different, the recipient of the program dynamic registration is only valid while the program is running, and the recipient of the XML registration will work regardless of whether your program has started. The method of dynamic registration in the program is introduced first.

    1. Dynamic Registration : Not a resident broadcast, which means that the broadcast follows the activity's life cycle. Note: Remove the broadcast receiver before the activity ends.
    2. configuration file Mode : Resident XML Manifest registration, that is, when the application is closed, if there is information broadcast, the program will be automatically run by the system call. Whether or not the application is started, the broadcast is monitored in the background as soon as it is powered on.

Android has a system broadcast can also customize the broadcast, to accept the broadcast information, then the broadcast receiver will have to be implemented by ourselves, we can inherit broadcastreceiver, we can have a broadcast receiver. There is a receiver is not enough, we have to rewrite broadcastreceiver inside the Onreceiver method, when to broadcast what we want to do, the following small program to display the broadcast application.

First, register (when the broadcast receiver is implemented, also set the broadcast receiver to receive broadcast information type, here is the information: Android.provider.Telephony.SMS_RECEIVED)

    1. Dynamic Registration method
      1. Generate broadcast processing   smsbroadcastreceiver = new Smsbroadcastreceiver ();   Instantiate the filter and set the broadcast to be filtered   intentfilter intentfilter = new Intentfilter ("Android.provider.Telephony.SMS_RECEIVED"); Registered broadcast   BroadCastReceiverActivity.this.registerReceiver (Smsbroadcastreceiver, Intentfilter);  
    2. Configuring broadcasts in Androidmanifest.xml
      1. <?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Spl.broadcastreceiver "android:versioncode=" 1 "android:versionname=" 1.0 "> & Lt;application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ".                  Broadcastreceiveractivity "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Androi D.intent.category.launcher "/> </intent-filter> </activity> <! --Broadcast registration--<receiver android:name= ". Smsbroadcastreceiver "> <intent-filter android:priority=" > <action Android:nam E= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>                </application> <uses-sdk android:minsdkversion= "7"/> <!--permission request--&      Gt    <uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission> </manifest>

Second, inherit Broadcastreceiver, rewrite the Onreceiver method. Here is the message sent by the monitor, will trigger the broadcast, and then the text content to parse out and show:
public class Smsbroadcastreceiver extends Broadcastreceiver    {         @Override public      void OnReceive (Context Context, Intent Intent)       {           Bundle bundle = Intent.getextras ();           Object[] Object = (object[]) bundle.get ("PDUs");           Smsmessage sms[]=new Smsmessage[object.length];           for (int i=0;i<object.length;i++)           {               Sms[0] = SMSMESSAGE.CREATEFROMPDU ((byte[]) object[i]);               Toast.maketext (Context, "the Message from" +sms[i].getdisplayoriginatingaddress () + "is:" +sms[i].getdisplaymessagebody (), Toast.length_short). Show ();           }           Terminate the broadcast, here we can slightly processing, according to the user input number can implement SMS firewall.           abortbroadcast ();       }          }  

  

      

      

      

Broadcastreceiver--android Broadcasting mechanism

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.