Reprint __ Broadcasting mechanism

Source: Internet
Author: User

Http://www.cnblogs.com/RMBP975/archive/2013/03/12/2955733.html

Introduction to Android broadcast mechanism

Broadcasting in Android is similar to radio broadcasting in the traditional sense, and the most important feature of broadcasting mechanism is that the sender doesn't care whether the receiver receives the data or not how the receiver handles the data. In addition, the broadcast can be received by more than one application, and of course it may not be received by any application.

three elements of the Android broadcast mechanism

The Android broadcast mechanism consists of three basic elements: Broadcast (broadcast)-used to send broadcasts, broadcast receivers (Broadcastreceiver)-for receiving broadcasts, intent content (Intent)-the medium used to hold information about the broadcast. Broadcast is a widely used mechanism for transmitting information between applications. Broadcastreceiver is a class of components that are filtered to receive and respond to the sent broadcast.

Broadcast (broadcast)

Android broadcasts can be divided into ordered broadcasts, general broadcasts (unordered broadcasts), and sticky broadcasts (stranded broadcasts).

1. General Broadcasting

The general broadcast is completely asynchronous, sent by the Context.sendbroadcast () method, and the message delivery is more efficient, but the order of execution of all receivers (receivers) is uncertain, and the disadvantage is that the receiver cannot pass the processing result to the next recipient. It is not possible to terminate the propagation of broadcast intent until there is no broadcast receiver matching it.

2. Orderly Broadcasting

Ordered broadcasts are sent through Context.sendorderedbroadcast (), and all receiver executes sequentially. When a broadcast receiver receives a broadcast, it can use the Setresult () function to pass the result to the next broadcast sink, then the GetResult () function to get the result returned by the last broadcast receiver, and the abort () function to let the system discard the broadcast. Use of the broadcast is no longer transmitted to other broadcast receivers.

Receiver priority can be set by setting the Android:priority property in receiver's intent-filter. 3. Sticky Broadcasting

Sticky broadcast is sent by means of the Context.sendstickybroadcast () method, the broadcast sent by this method is stuck (waiting), and the broadcast receiver receives this broadcast when a broadcast receiver that matches the broadcast is registered and the broadcast receiver is active. When you use this method to send a broadcast, you need to obtainBROADCAST_STICKY权限。

The Sendstickybroadcast only retains the last broadcast and retains it, so that even if a broadcast receiver has already processed the broadcast, the broadcast will still be received when a matching broadcast receiver is registered. If you only want to process the broadcast again, you can pass the Removestickybroadcast () method.

Receive priority for broadcast

Ordered broadcasts: If a statically registered broadcast sink has a higher priority than a dynamically registered broadcast receiver, then the statically registered broadcast receiver receives the broadcast first

Out of order broadcast: dynamically registered broadcast receivers high priority > dynamically registered broadcast receivers low priority > statically registered Broadcast receivers high priority > statically registered broadcast receivers low priority

There is a pervasive principle in the order in which broadcast receivers of equal priority receive broadcasts:

1) dynamic registered broadcast receivers of equal priority, first registered first receive

2) static receivers of equal priority, with the order of receiving broadcasts consistent with string[] java.io.File.list ()

The result is from: http://su1216.iteye.com/blog/1747706

broadcast Receivers (Broadcastreceiver)

Broadcastreceiver (broadcast receiver) a component that listens to changes in an application's operating environment and responds to change events, broadcast reciver and event-handling mechanisms, unlike event-handling mechanisms that are application component-level, For example, a button's Onclicklistener event can only be handled in one application. Broadcast event handling mechanisms are system-level, and different applications can handle broadcast events.

We can also develop broadcastreceiver in our own applications, and then register the broadcast receiver class or object to the Android operating system, letting the operating system know that there is now a broadcast receiver waiting to receive broadcasts from the Android operating system, That is, implement Broadcastreceiver in your own application to listen for and respond to the intent of the broadcast.

When there is a broadcast event, the Android operating system first tells the Register to its above the broadcast receiver generated a kind of event, each receiver first judge is not my this receiver needs the event, if it is required by the event, and then the corresponding processing.

1.Register Broadcastreceiver

Broadcastreceiver is used to listen for broadcast events (Intent), in order to achieve this, broadcastreceiver must register, the method of registration has the following two kinds:

1) static registration (resident broadcast)

The static registration method is to define receiver in the Androidmanifest.xml application and set the action to be received. The feature is that monitoring is done regardless of whether the application is active or not.

1 <receiver android:name= "Myreceiver" >2     <intent-filter>3         <action android:name= " Myreceiver_action "/>4     </intent-filter>5 </receiver>   

<intent-filter> tag sets the filter to receive the specified action broadcast.

2) Dynamic registration (very broadcasting)

Dynamic registration method in the activity call function to register, and static content is similar. One parameter is receiver and the other is Intentfilter, which is the action to receive. The feature is that after registering in the code, when the application shuts down, it is no longer listening.

1//Create a broadcast recipient2 Myreceiver receiver =NewMyreceiver ();3//Create a filter and specify the action to be used to receive broadcasts for the same action4 Intentfilter filter =new intentfilter ("myreceiver_action" 5 // registered broadcast receiver 6 Registerreceiver (receiver, filter); 
2. Send a broadcast

Creates a intent (intent) object, specifies the intent action, sends the broadcast through the Sendbroadcast (mintent) method, and can carry parameter data through intent.

1//Specify the broadcast target action2 Intent Intent =New Intent ("Myreceiver_action");3//can carry messages via intent4 Intent.putextra ("MSG", "Send Broadcast");  5 // send broadcast message 6 sendbroadcast (intent);     
3.Logout Broadcastreceiver

A broadcast receiver that is registered statically does not require a manual logoff, and an instance of the object is destroyed at any time after OnReceive is called, and the broadcast receiver is registered dynamically. You need to manually log out using the Unregisterreceiver method when the activity enters the stop or destroy state.

deregistration of the broadcast receiver 2 unregisterreceiver (receiver); 

In general, Broadcastreceiver objects are registered in OnStart and logged off in OnStop.

4.the life cycle of BroadcastreceiverThe Broadcastreceiver object is valid only if it is called OnReceive (Context, Intent), and when returned from the function, the object is invalid and ends the life cycle.

A process that has an active broadcast receiver is protected from being killed, but a process that has a deactivation component will be killed at any time when other processes require the memory it occupies. Therefore, if it takes a long time to respond to a broadcast message, we will generally include it in a derived thread rather than completing it within the main thread, thus ensuring a smooth user interaction process.
1PublicClass MyreceiverExtendsBroadcastreceiver {2@Override3PublicvoidOnReceive (context context, Intent Intent) {4 // accept post-broadcast processing logic 5 } 6}     
Myreceiver is a class that inherits Broadcastreceiver, overrides the Onreceiver method, and processes the broadcast in the Onreceiver method.

Reprint __ Broadcasting mechanism

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.