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
On June 27, Apple released the public beta of iOS...
Introduction to the novel of the widowed sister-i...
Live quiz show was first started by HQTrivia in t...
The price list for renting overseas server bandwi...
Produced by: Science Popularization China Produce...
A few days ago, we held a small activity on the f...
iPhone to Android: But what about iMessage? Nowad...
What? Reading and understanding this article can ...
The cost remained at 180 a few days ago, why did ...
Reviewer: Yang Rongya, Chief Physician, Seventh M...
It is an indisputable fact that the Internet has ...
The trouble with public accounts: disappearing re...
At the beginning, my public account only had doze...