The Android series broadcast

Source: Internet
Author: User

For system-level message notifications, Android introduces the concept of broadcast. And the broadcast in Android is more flexible because the application can publish its own broadcasts and receive broadcasts. Sending broadcasts requires the use of intent to send, while receiving broadcasts requires the Broadcastreceiver class to receive broadcast operations by overloading the OnReceive method.
First, the receiving system broadcasts. How to receive system broadcasts, in this there are 2 ways, dynamic and static.
Dynamically received, a new class inherits from Broadcastreceiver and implements the OnReceive method, but how does the receiving broadcast subclass know which broadcast to receive? Then you need to add the corresponding action through the Intentfilter class, and then pass the activity's Registerbroadcast method to the receiving broadcast subclass and the Intentfilter object to register the action to receive the broadcast into the app. This enables the operation to receive the broadcast, and then what kind of broadcast is received is determined by the action added by Intentfilter. Of course, if you want to invoke the functionality of the system, you need to add the appropriate permissions in the Androidmanifest file.
So what is static reception? Dynamic receive is required when the application is opened to receive, but what if I need to listen to some broadcasts but do not need the program to start? For example, listen to the system to start the broadcast, this time need to receive the broadcast statically. Static receive also requires a new subclass to inherit Broadcastreceiver and implement the OnReceive method. The rest is handed to the Androidmanifest file, which adds the receiver tag (where all broadcasts are received), and completes the broadcast reception by specifying the name of the receiver and the action of the intent-filter tag.
The above describes the reception of the broadcast, then we can build a broadcast to send it? The answer is yes. It says that it is necessary to use intent to send a broadcast. The broadcast is sent by passing the broadcast type parameter in intent and then calling the Sendbroadcast method to send the broadcast. Of course, there are different modes of broadcasting, and there are 2 modes of broadcasting in Android: unordered and orderly. Unordered mode as described above, as long as a receiver listening to the broadcast will receive the broadcast at the same time. But orderly broadcasts have their priorities. The orderly send broadcast is also very simple, it needs to replace the Sendbroadcast method with the Sendorderedbroadcast method. However, this method has 2 parameters, the first is still the intent object, and the second is a permission-related string.
So sometimes we want to send the broadcast and receive broadcasts are only inside the program, external to the program broadcast a little relationship is not, then how to do? This requires the use of local broadcasts. By using the Localbroadcastmanager class, the corresponding Sendbroadcast and Registerbroadcast methods are called to send and receive broadcasts. (using the Localbroadcastmanager.getinstance method to obtain the Localbroadcastmanager instance).
At this point, all the contents of the broadcast are described.

The Android series broadcast

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.