Android: Broadcast receiver (BroadCastReceiver .,
@ DescriptionThe broadcast receiver can receiveContext.sendBroadcastOr the intent (intent) sent by Context. sendOrderedBroadcast ).@ LocalBroadcastManagerWe recommend that you useLocalBroadcastManager,LocalBroadcastManager is a help class to help you convenientlyCurrent ApplicationTo register and send broadcasts. In addition, it has the following advantages over sending global broadcasts:1. Do not worry about the security of private data, because the broadcast sent will not be received by other applications except the current application.2. Do not worry that other applications will exploit your security vulnerabilities because other applications cannot passLocalBroadcastManager sends you a broadcast.3. It is more efficient than sending global broadcasts. Common Methods for LocalBroadcastManager are as follows:
@ RegisterThe broadcast receiver can dynamically register through Context. registerReceiver (), orAndroidManifest.xmlAdd<receiver>Static tag registration. Dynamic Registration Method: If youActivity. onResume () registers a broadcast receiver, you should Activity. onPause () unregister it. (When the Activity Pause, you will not receive the broadcast. If you are notCancellation in Activity. onPause () will cause a great amount of resource consumption.). Do notCancels a broadcast receiver in Activity. onSaveInstanceState ().Because this method is not called when the user moves to another Activity in the history stack. Static registration method: Here, priority is used to set the priority. The type is Integer and the range is-1000 ~ 1000. in this range, the greater the value, the higher the priority. Example: Customize three broadcast receivers. 1, 2, 3, 4, register in the configuration file, and set their priority to 1000,500, 1; 5, and send broadcast through Context. sendBroadcast. Result:@ Broadcast type:1. Normal broadcast (Normal broadcasts): Normal broadcasts are sent by the Context. sendBroadcast method. This method is completely asynchronous. In this mode, all broadcast receivers run in an unordered state, and often all receivers receive broadcast at the same time. This method is more efficient, but cannot be interrupted. 2. Ordered broadcast (Ordered broadcasts): Ordered broadcast is composed of Context. the sendOrderedBroadcast method can only receive messages from one receiver at a time. All receivers receive broadcasts in turn, so they can either spread a result to the next receiver or completely interrupt a broadcast. The order of acceptance is determined by the priority in the configuration of the configuration file. The range is (-1000 ~ 1000) the higher the value, the higher the priority. receivers with the same priority receive broadcasts in any order.@ Think about security.Considering that broadcast receivers are the feature of an application, we should consider that our own receivers are not abused by other applications. 1. The Intent namespace is global, so the Intent action name and other String parameters must consider uniqueness. Otherwise, we may not intend to conflict with other applications. 2. When registering a broadcast receiver dynamically, you can use the permission to restrict who can send the broadcast to us. 3. During static registration, any broadcast that meets your specified intent-filters can send you a broadcast. We can use the specified android: exported = "false" attribute to prevent other broadcasts from being sent to you.@ Broadcast receiver lifecycle note1. A broadcast receiver object is only in itsThe onReceive (Context, Intent) method is valid only when it is called.The code of the onReceive (Context, Intent) method is returned, and the broadcast receiver object is no longer valid. The main impact is that you canAny asynchronous operations are useless when the onReceive (Context, Intent) method is executed,The onReceive (Context, Intent) method has been executed, and the broadcast receiver object is no longer valid or recycled by the system.2. Normally, you cannotThe onReceive (Context, Intent) method pops up a dialog box or binds a service. For the former, you can useNotificationManagerFor the latter, you can callContext.startService()Method. Time-consuming operations should be performed in the Service.