PrefaceEach application in Android can register for the broadcasts it is interested in, so that the program will only receive the broadcast content it is interested in, which may come from the system or from other applications; Android provides a complete set of APIs that allow applications to freely send and receive broadcasts. Today we will introduce in detail 1. Broadcast Introduction1. Standard broadcast
2. Orderly broadcast
3. Sticky Broadcast
2. Broadcasting DetailsAndroid has many built-in system-level broadcasts. We can listen to these broadcasts in the application to get various system status information. For example, a broadcast will be sent when the phone is turned on, a broadcast will be sent when the battery level changes, and a broadcast will be sent when the time or time zone changes, etc. There are generally two ways to register broadcasts: Dynamic registration is registered in the code; Static registration is registered in AndroidManifest.xml 1. Static registration Generally, it is a permanent broadcast, which is set in AndroidManifest.xml through
The intent filter specifies the action to which the receiver subscribes; 2. Dynamic RegistrationNon-resident broadcasts are registered when used and destroyed immediately after use;
Remember to log out in time to avoid memory leaks;
3. Custom broadcastRegister Broadcast
4. Local broadcastAll broadcasts we send and receive belong to the system global broadcast, that is, the broadcasts sent can be received by any other application, and we can also receive broadcasts from any other application; In order to solve the broadcast security problem, Android introduced a local broadcast mechanism. The broadcasts sent using this mechanism can only be transmitted within the application, and the broadcast receiver can only receive broadcasts from this application, so all security issues do not exist; Initialize broadcast:
advantage:
5. Standard broadcast with permissionsSend a broadcast When calling sendBroadcast(Intent, String) or sendOrderedBroadcast(Intent, String, BroadcastReceiver, Handler, int, String, Bundle), you can specify the permission parameter;
To receive this broadcast, the receiving app must apply for this permission
Receiving Broadcasts If you specify a permission parameter when registering a broadcast receiver (using registerReceiver(BroadcastReceiver, IntentFilter, String, Handler) or in the manifest file Declare in the manifest file:
When registering, declare:
To send a message to it, the app that broadcasts the message must apply for the corresponding permissions:
SummarizeDynamically registered broadcasts are not permanent broadcasts, which means that the broadcast follows the life cycle of the Activity. Note that before the Activity ends, remove the broadcast receiver; Static registration is permanent, which means that when the application is closed, if there is a message broadcast, the program will be called by the system to run automatically; When the broadcast is an ordered broadcast: the one with the highest priority is received first (regardless of static or dynamic). For broadcast receivers with the same priority, dynamic takes precedence over static; For the same type of broadcast receivers with the same priority, static: the first scan takes precedence over the later scan; dynamic: the first registration takes precedence over the later registration; When the broadcast is the default broadcast: regardless of the priority, dynamic broadcast receivers take precedence over static broadcast receivers. For the same type of broadcast receivers with the same priority, static ones scanned first take precedence over those scanned later, and dynamic ones registered first take precedence over those registered later; This article is reproduced from the WeChat public account "Android Development Programming" |
>>: Profit as soon as you buy it: iPhone 13 depreciates 50% lower than iPhone 12
Starting with the e-tron, Audi's first electr...
This article will share with you the techniques f...
The 2020 Annual Meeting of the Second-Hand Car Br...
This article briefly introduces the early develop...
Currently, Alipay is one of the most mainstream m...
There is nothing better than having a hot pot in ...
In this world, although everyone's height and...
Speaking of Wenchang Tower, I believe many people...
[[146504]] When programmers see the concept of fu...
With the New Year being such a hot topic, every o...
Recently, I have read books on "market trend...
For all our promotions , we must identify the tar...
What is it like for a brand to do marketing on Zh...
In today's Internet age, if you have fans, yo...
The impression of Internet cafes has gone through...