Android Four components--broadcastreceiver General broadcast, ordered broadcast, intercept broadcast, local broadcast, sticky broadcast, system broadcast

Source: Internet
Author: User

is a basic summary of the article, personal obsessive-compulsive disorder, must be the four main components of the blog to be filled, summed up the various uses of boradcastreceiver, nonsense said, driving. bloggers suggest to knock over the code to understand the broadcast, because there are many details of things to pay attention to, in the study Bo master also made some low-level errors, or later to large projects, do not firmly grasp the good foundation, will waste a lot of time on this. or read the content of this article carefully

Broadcastreceiver translated as a broadcast receiver, broadcast is a widely used mechanism for transmitting information between applications, which can be easily understood as a traditional radio broadcast, a popular point, the publication of Lost and found

The broadcast mechanism is a typical publish-subscribe pattern, which is what we call the Observer pattern. The most important feature of broadcasting is that the sender does not care whether the receiving party receives the data, nor cares about how the receiver handles the data, and achieves the complete decoupling of the receiving and receiving parties by this form.

The general broadcast is completely asynchronous and is sent through the context's Sendbroadcast () method, which is more efficient, but the order of execution of all receivers (receivers) is uncertain. The disadvantage is that the receiver cannot pass the processing result to the next recipient and cannot terminate the propagation of the broadcast intent until there is no broadcast receiver matching it. The following is a demonstration of a custom normal broadcast

First, create a broadcast

Creating a broadcast is as simple as inheriting broadcastreceiver and implementing the OnReceive () method

II. Registration of broadcasting

Broadcastreceiver is one of the four major components, so there is no doubt that you need to register, Broadcastreceiver registration There are two ways:

    • Configuration via manifests
    • Dynamic configuration via code

1, method One: through manifests configuration

Here we need to add the name attribute in Intent-filter's action to indicate what we are listening to. When a broadcast is sent, it is necessary to determine whether the broadcast is consistent with what we listen to, and if it is consistent, receive

2, method Two: Dynamic configuration through the code

Third, anti-registration broadcasting

If you are using a dynamic registration broadcast, you need to unregister the broadcast when the activity is OnDestroy

Iv. Sending broadcasts

Here we use a button to send the broadcast, send our created intent custom broadcast via the Sendbroadcast () method

Five, run the code

After running the program, we click Send Broadcast. We use log information to verify that the broadcast was received by US accurately.

Ordered broadcasts are sent through Context.sendorderedbroadcast (), all broadcast receiver priorities are executed sequentially, and broadcast receivers are prioritized via the receiver's Android in Intent-filter: Priority property to set, the higher the value the higher the precedence.

When a broadcast receiver receives a broadcast, it can use the Setresult () function to pass the result to the next broadcast sink, and then the GetResult () function to get the result returned by the last broadcast receiver. When a broadcast receiver receives a broadcast, it can also use the Abortbroadcast () function to intercept the broadcast and discard the broadcast so that the broadcast is no longer transmitted to another broadcast receiver

First, create a broadcast

We create a class that holds three priority broadcast receivers and delivers results to the next broadcast in the highest-level broadcast

It is important to note that:

    1. The broadcastreceiver of the inner class must be modified by the public static, otherwise the error will be

II. Registration of broadcasting

The registration method here is the same as the normal broadcast, where the difference is the priority attribute, which determines the precedence between them.

It is important to note that:

    1. The Broadcastreceiver class name is separated from the name of the inner class with a $ symbol, or an error is made

Third, send the broadcast

And not the same place as before, here is the use of sendorderedbroadcast () to send an ordered broadcast

It is important to note that:

    1. It is necessary to send an ordered broadcast here, otherwise the receiver will get an error through the Setresult () and the GetResult () method, because only an ordered broadcast can pass the result

Four, run the code

After running the program, we click Send Broadcast. We use log information to verify that the broadcast is received accurately by us, and that the data is transmitted to us accurately.

We mentioned above that the broadcast can be intercepted in an orderly broadcast, then we modify the code on the basis of the above program, middle-aged clothing in the Highpriority receiver plus interception broadcast

First, create a broadcast

By executing the Abortbroadcast () method in Broadcastreceiver, the broadcast will not continue to pass down the

Second, run the code

After running the program, we click Send Broadcast. We're using log information to verify that we intercepted the broadcast.

As you can see, the back of mid and low broadcasts have no log information to indicate that we have successfully intercepted

Third, the expansion of orderly broadcasting and interception of broadcasting--end broadcasting

Now there is such an application scenario, according to the above procedure, can only be intercepted in the first broadcast, the subsequent broadcast will not be executed. If at this time we need a broadcast that must be executed, whether or not it is intercepted, we call it the end of the broadcast. Similarly, sending an ordered broadcast takes this into account, sending the broadcast through the following code, and specifying that we have to execute the end broadcast regardless of whether or not it is intercepted

Run the code, we view the log information

It can be found that there is only high log information, because it is intercepted, and the log information has a low, indicating that we intercept, but also to perform the end of the broadcast

Added local broadcast in API21 's support V4 package, also known as Localbroadcastmanager. Since the previous broadcasts are global and all applications can receive them, this poses a security risk, so we use Localbroadcastmanager only to send information broadcasts within our app, restricting the use of in-process

It is very simple to use, only need to call the context of the Sendbroadcast, Registerreceiver, Unregisterreceiver place for Localbroadcastmanager.getinstance ( The corresponding function in the context context. The process of creating a broadcast here is the same as a normal broadcast, and there is not much to introduce

First, register receiver

Second, anti-registration receiver

Third, send asynchronous broadcast

Iv. Sending synchronous broadcasts

Sticky broadcasts are sent through the Context.sendstickybroadcast () function, and broadcasts sent with this function remain stranded, and the broadcast receiver receives this message when a broadcast receiver that matches the broadcast is registered. You need to get broadcast_sticky permission when you use this function to send a broadcast

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 once, you can do so through the Removestickybroadcast () function. The process of creating a broadcast here is the same as a normal broadcast, and there is not much to introduce

Of course, there will be a lot of broadcast in the system, when certain conditions are met, the system will send some well-defined broadcasts, such as: restart, charging, telephone calls and so on. We can listen to our system broadcasts through the Action property.

The process of creating a broadcast here is the same as a normal broadcast, and there is not much to be described here. The popular broadcast action properties are

    • Broadcast after the screen is closed: Intent.action_screen_off
    • Broadcast after the screen is opened: intent.action_screen_on
    • Charging status, or battery power change: intent.action_battery_changed
    • Broadcast when you turn off or turn on airplane mode: intent.action_airplane_mode_changed
    • Indicates low battery power: intent.action_battery_low
    • Indicates that the battery is fully charged, which will emit a broadcast when the battery is full: Intent.action_battery_okay
    • Broadcast when pressing the camera button (hardware key) when taking a photo: Intent.action_camera_button

Android Four components--broadcastreceiver General broadcast, ordered broadcast, intercept broadcast, local broadcast, sticky broadcast, system broadcast

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.