Android broadcast mechanism and usage explanation (RPM)

Source: Internet
Author: User

Ext.: http://blog.sina.com.cn/s/blog_5da93c8f010178zl.html

Reference: Http://blog.sina.com.cn/s/blog_80723de801014e2g.htmlhttp://blog.csdn.net/jjaze3344/article/details/7259272first, what is broadcast? There are all kinds of broadcasts in Android, such as the use of batteries, the reception of telephones and the acceptance of text messages to produce a broadcast, and the application can accept broadcasts and process them logically. II. types of broadcastingBroadcastreceiverThe corresponding broadcasts are divided into two categories: Regular broadcasts and orderly broadcasts . 1, the general broadcast is completely asynchronous, can be received at the same time (logically) by all receivers, the efficiency of the message delivery is higher, but the disadvantage is: the receiver cannot pass the processing results to the next recipient, and cannot terminate the broadcast intent propagation. General Broadcastthroughcontext.sendbroadcast ()method to send,all receivers receivers receive broadcast in an indeterminate order. This approach is more efficient, but broadcastreceivercannot be usedSetresult Series,GetResult SeriesandAbort Series API. 2. Orderly broadcasting is the priority level declared by the recipient, and the receiver receives the broadcast in turn. If the level of a is higher than b,b, then the broadcast is passed to a, then to B, and finally to C. The precedence level is declared in the android:priority attribute of the intent-filter element, the higher the number the greater the precedence level. you can set the priority of receiver by setting the android:priority property in Intent-filter. Receiver with the same priority has an indeterminate order of execution. If Broadcastreceiver is registered in the code and its intent-filter has the same android:priority attribute , first register will receive the broadcast first.

The receiver of an orderly broadcast can terminate the broadcast intent, and once the broadcast intent is transmitted, the receiver will not be able to receive the broadcast. Orderly Broadcastis throughContext.sendorderedbroadcastto send, all receiver is executed sequentially. The system executes the recipient sequentially, according to the priority level declared by the receiver, and the preceding receiver has the right to terminate the broadcast (Broadcastreceiver.abortbroadcast ()), and if the broadcast is terminated by the previous receiver, the subsequent recipient will no longer be able to obtain the broadcast. For ordered broadcasts, the previous receiver can store the data in the result object via the Setresultextras (bundle) method, then pass it to the next recipient, the next recipient through code: Bundle bundle = Getresultextras (True)) You can get the data that was stored in the result object by the previous recipient.

The radio that receives text messages here is ordered broadcast, so you can set your own broadcast receiver level higher than the original system level, you can intercept text messages, and do not save the inbox, there will be no letter tone.

The implementation method is:

<receiver android:name= ". Smsreceiver ">
<intent-filter android:priority= "2147483647" >
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

Inside the android:priority= "2147483647", is to set the level of the broadcast receiver, the priority of the intent-filter tag is to set a broadcast receiver priorities, many online information is expressed, The priority setting value is 1000~-1000,1000 maximum, but in fact, when the priority value is the maximum value of integer, it is the highest precedence, that is, 2147483647; of course, "Max" is limited to static registration.

Two ways to register Broadcastreceiver:

1. Registering in the application's code
registration: Broadcastreceiver

intentfilter filter = new Intentfilter ("Android.provider.Telephony.SMS_RECEIVED");    

Filter.setpriority (2147483647);

Receiver=new Broadreceiver ();

Registerreceiver (Receiver,filter);

Cancel Registration Broadcastreceiver
Unregisterreceiver (receiver);

2. Registering in Androidmanifest.xml

<receiver android:name= "Smsreceiver" >
<intent-filter android:priority= "2147483647" >
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

Android broadcast mechanism and usage explanation (RPM)

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.