Android: Broadcast mechanism

Source: Internet
Author: User

I remember when I went to school, each class will have a speaker in the classroom, these speakers are connected to the school's broadcast room, once there is any important notice, will broadcast a radio to inform the teachers and students. Similar working mechanisms are also widely used in the computer field, if you understand the principle of network communication should be aware that in an IP network range of the largest IP address is reserved as a broadcast address to use. For example, the IP range of a network is

192.168.0.XXX, the subnet mask is 255.255.255.0, then the broadcast address of this network is 192.168.0.255. The broadcast packets are sent to all ports on the same network, so that each host in the network receives the broadcast.

To facilitate system-level message notification, Android has introduced a similar set of broadcast messaging mechanisms. Compared to the two examples I cited earlier, the broadcast mechanism in Android will appear more flexible, and this chapter will explain in detail the aspects of this mechanism.

Why is the broadcast mechanism in Android more flexible? This is because every app in Android can register its own broadcasts of interest, so the program will only receive the broadcast content that it cares about, either from the system or from other applications. Android provides a complete set of APIs that allow applications to freely send and receive broadcasts. The way to send a broadcast is actually slightly mentioned before, if you have a good memory, you may still have an impression, is the use of our 2nd Chapter learned Intent. The method of receiving the broadcast requires the introduction of a new concept, the broadcast receiver (broadcast receiver).

The specific usage of the broadcast receiver will be described in the next section, where we'll first look at the type of broadcast.

Broadcast in Android can be divided into two types, standard broadcast and ordered broadcast.

Standard broadcast (normal broadcasts) is a fully asynchronous broadcast that, after broadcast, all broadcast receivers receive this broadcast message almost at the same time, so there is no sequencing between them. This kind of broadcast is more efficient, but it also means that it cannot be truncated. The workflow for standard broadcasts is shown in Figure 5.1.

Figure 5.1

Ordered broadcast (Ordered broadcasts) is a synchronous broadcast, after the broadcast, only one broadcast receiver will be able to receive this broadcast message at the same time, and when the logic in this broadcast receiver is completed, the broadcast will continue to pass. So at this time the broadcast receiver is in order, the priority of the broadcast receiver can receive a broadcast message, and the front of the broadcast receiver can also truncate the broadcast is being delivered, so that the subsequent broadcast receivers will not be able to receive broadcast messages. The orderly broadcast is shown in Workflow 5.2.

Figure 5.2

Android: Broadcast 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.