Android BroadcastReceiver
Description: broadcastReceiver is one of the four major android components, and most of the broadcasts are broadcast by the system. For example, the screen is off and the battery is insufficient. Applications can also create broadcasts. For example, when downloading is complete, you need to use broadcastreceiver to let other applications know about this situation. The receiver has no interface, it may create a status bar notification to notify users. Broadcastreceiver only does some very small work. For example, it can start a service job.
The code of the base class will receive Intents sent by sendBroadcast.
If you do not need to send broadcasts between applications, you can use LocalBroadcastManager instead of the method described below. This method has better performance and does not need to consider security issues between different applications, because other applications may be able to receive the broadcast.
You can use Context. registerReceiver () to dynamically register a consumer or use the <author ER> element to statically register a consumer in the AndroidManifest. xml file.
Note: If you register a handler in Activity. onResume (), you must cancel the handler in the Activity. onPause () method. Do not log out of the handler in the Activity. onSaveInstanceState () method, because it is not called when the user returns to the history stack.
There are two main types of broadcasts that can be received:
Normal broadcasts (sent by Context. sendBroadcast) are asynchronous. All schedulers are ordered at the same time. This method is more efficient, which means that the consumer cannot use the result or terminate the API.
Ordered broadcasts (sent by Context. sendOrderedBroadcast) are sent to a receiver at the same time. Because each worker is executed in sequence, the result can be passed to the next worker, or it can completely terminate the broadcast, and the broadcast is not passed to the next worker. The execution sequence of ordered referers is related to android: priority. This attribute can control the execution sequence of ordered referers. If referers have the same priority, their execution sequence is arbitrary.
Even in normal broadcast, the system sometimes sends the broadcast to a worker at the same time. Under special conditions, you may need to create a process to process the worker. At a certain point in time, only one worker will run. To avoid overload, the system can create new processes. In this case, however, these schedulers still cannot return results or terminate their broadcasts.
Note that even if the Intent class is used to send and receive broadcasts, the Intent broadcast mechanism is completely different from the one used to start the activity. BroadcastReceiver cannot process Intent sent by startActivity () method. This mechanism is not available. Similarly, when an Intent is broadcast, an activity cannot be found or started. These two operations are completely different in semantics: starting an activity through Intent is a foreground operation, which changes with the user's operations; broadcasting an Intent is a background operation that users cannot realize.
There are three topics as follows:
Security
Receiver Lifecycle
Process Lifecycle
Security
Scheduler is usually a tool for communication between applications, so you must consider how other applications may abuse them. The following issues need to be considered:
The Intent namespace is global. Make sure that the defined action name and other strings belong to your own application, otherwise it will unconsciously conflict with other systems.
If you use registerReceiver (BroadcastReceiver, IntentFilter) to register a Receiver, any application may send a broadcast to that receiver. You can define permissions to control who can send broadcasts to it.
In manifest, the consumer er is defined and intent-filters is defined. Any application can ignore the specified filter conditions and send a broadcast to it. To prevent other applications from sending broadcasts to it, define android: exported = "false" in manifest, so that other applications cannot send broadcasts to this receiver.
Use sendBroadcast (Intent) or related methods. Normally, other applications can receive this broadcast. You need to define the permission to control the receiver who can accept the broadcast. Alternatively, starting with ICE_CREAM_SANDWICH, you can specify the receiver by setting Intent. setPackage.
With LocalBroadcastManager, there will be no such problem. After the broadcast is sent, no current process will occur.
Both receiver and broadcast can set access permissions.
To execute permission during sending, a non-empty permission parameter must be provided to sendBroadcast (Intent, String) or sendOrderedBroadcast (Intent, String, BroadcastReceiver, android. OS. handler, int, String, Bundle ). the receiver that only defines this permission can receive broadcasts (AndroidManifest. <uses-permission>) is defined in xml ).
When you receive the execution permission, you must provide a non-empty permission parameter when registering the receiver-or when you call registerReceiver (BroadcastReceiver, IntentFilter, String, android. OS. handler) or in AndroidManifest. the xml file defines a static <er ER>. only broadcasts granted with a specified permission can be sent to the receiver.
Receiver Lifecycle
The BroadcastReceiver object is valid only when the onReceive (Context, Intent) method is called. Once it is returned from this method, the system will think that this object has been completed and is not in the active state.
Here, when onReceive (Context, Intent) is implemented, note: any operation that requires asynchronous processing is not feasible, because it needs to be returned from the method, but the method must process asynchronous operations, in this case, the BroadcastReceiver process may be killed before the asynchronous operation is completed.
In special cases, BroadcastReceiver may not display a dialog or bind a service. For the former, use the icationicationmanager API instead. For the latter, use Context. startService () to send a command to the service.
Process Lifecycle
The currently running BroadcastReceiver (running on the onReceive (Context, Intent) method) process must be considered as a foreground process, it will always run unless the system memory is at its limit (the system will recycle the memory ).
Once returned from onReceive (), BroadcastReceiver is no longer in active state, and its running process is as important as other running application components in it. This is very special and important, because the process only serves BroadcastReceiver, and then the receiver returns from onReceive (), the system will think that the process is empty and will kill it as soon as possible to recycle resources.
This means that for a long-running operation, it is best to use the service and BroadcastReceiver in combination to keep the process running permanently throughout the operation.