In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about the practice of the broadcast message mechanism in Android. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The Android system has a set of broadcast message mechanism to facilitate message notification between each application. And broadcast receiver, as one of the four components of Android, is often used. I am also exposed to the broadcast message mechanism, so it is necessary to sort out what I have learned.
0x01 broadcast classification
Standard broadcast
A standard broadcast is a completely asynchronous broadcast. This kind of broadcast is an one-to-many broadcast with high efficiency. When the broadcast is sent, all the radio receivers will receive the message at almost the same time.
Orderly broadcast
An orderly broadcast is a broadcast executed synchronously. As its name implies, this kind of broadcast is broadcast in order of acceptance. When the broadcast is sent, only one broadcast recipient will receive the message. Therefore, the broadcast is an one-to-one broadcast.
0x02 registered broadcast
Dynamic registration
Register with the Java code. Broadcast recipients are free to register the broadcasts they are interested in and handle the logic that responds to the broadcasts internally. Although this approach is flexible, it also has some limitations. It requires the application to proceed at run time.
Static registration
Register with AndroidManifest.xml. This allows you to register to receive broadcasts before the application is started.
Register Classic Code dynamically
Static registration of classic code
Add the following code to the Application node of the AndroidManifest file:
The recipient of the broadcast is named MyBroadcastReceiver. It has two properties: the enabled property indicates whether the broadcast recipient is enabled, and the exported property indicates whether the broadcast recipient is allowed to receive broadcasts outside this program.
The difference between 0x03 static registered broadcast and dynamic registered broadcast
Static registration can still receive a broadcast even if the App exits
When registering dynamically, when Activity exits, the broadcast will not be received.
But the idea that static registrations can still receive broadcasts even if App exits may no longer be true since Android 3.1.
Description:
Android 3.1The system has added parameters in Intent broadcast-related flag:
1) FLAG_INCLUDE_STOPPED_PACKAGES: contains packages that have been stopped (stop: that is, the process in which the package is located has exited)
2) FLAG_EXCLUDE_STOPPED_PACKAGES: does not contain packages that have been stopped
Since Android3.1, the system itself has added tracking of whether all App is currently running. When sending a broadcast, no matter what type of broadcast it is, the system directly increases the flag with the value of FLAG_EXCLUDE_STOPPED_PACKAGES by default, so that even the statically registered broadcast receiver cannot receive the broadcast for the App whose process has exited.
Therefore, for system broadcasts, the value of this intent cannot be changed because it is sent directly from within the system. Therefore, for a statically registered BroadcastReceiver that receives a broadcast from the system, it will not be able to receive the broadcast if the App process has exited.
However, for custom broadcasts, you can override the flag to FLAG_INCLUDE_STOPPED_PACKAGES, so that the statically registered BroadcastReceiver can receive the broadcast even if the App process has exited, and will start the application process, but the BroadcastReceiver is newly created.
The implementation code is:
Before 3.1, many App may listen to various system broadcasts through static registration, so as to carry out some business processing (for example, even if App has exited, it can still be received, service can be started, etc.).
After 3.1, static registration will accept the change of broadcasting mode, which will directly lead to the infeasibility of this kind of scheme. Therefore, by setting Service and App itself as different processes, it has become a viable alternative to achieving such requirements.
0x04 sends broadcasts
Send a standard broadcast
Send an orderly broadcast
You need to determine the order in which broadcasts are accepted by setting the priority property of the intent-filter node of the broadcast recipient in the AndroidManifest file.
If you want to truncate the broadcast, call abortBoradcast () in onReceive ()
0x05 uses local broadcasting
In order for broadcasts to be delivered only in applications, this requires LocalBroadcastManager to show its skills. We are familiar with it through the code.
Best practices for 0x06 broadcast
Broadcasts can be applied to logical operations that force offline or be squeezed offline.
The above is the practice of the broadcast message mechanism in Android shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.