- As one of the four components of Android, broadcast receiver is widely used in the whole system, there are various broadcasting mechanisms in the system, such as downloading, network, etc. have specific broadcast receivers
- Broadcast to a large extent simplifies the development, can be broadcast monitoring system status changes, listening to another app event delivery, you can receive the current app in different components, different UI, different threads between the message delivery
- Creation and use of broadcasting
- Create a subclass that inherits from the Broadcastreceiver class, overriding the Onreceiver method
Public class Mreceiver extends Broadcastreceiver { @Override publicvoid onreceive ( Context context, Intent Intent) { //ToDo }}
- Note that in OnReceive can not handle time-consuming operations, generally not more than 6s, more than 6s will be reported exception exception, if there are time-consuming operations, are put into the worker thread to run, the worker thread specific reference to Android-based multi-threading development
- You need to register the receiver
- receiver node
- property in
- manifest: Name: Specify receiver classpath
- child node Intent-filter
- Property: Priority: Precedence, -1000~1000, high priority to receive broadcasts early, only sendorderedbroadcast send with
- child nodes
- action: Identity, prepared for implicit intent calls, can receive broadcasts across apps
<receiver android:name=". Mreceiver" android:exported="true"> <intent-filter android: priority="> <action android:name=" Com.example.AndroidTest_01.receiver1"></action> </intent-filter></ Receiver>
- After registration is not enabled when the app is launched, it is running in the system, regardless of whether the app is open or exited, is enabled, and executes onreceiver after receiving the message, even though the app has exited, so if you need to interact with the app, you need to be aware
- Receiving broadcasts across applications or receiving system broadcasts requires setting Android:exported=true;
- Send broadcast
- Create a broadcast intent
- To create an intent explicitly
Intent intent1=New Intent (myactivity. this, mreceiver. class ); Sendbroadcast (intent1);
- Implicitly creating intent
Intent intent1=New Intent (); Intent1.setaction ("Com.example.AndroidTest_01.receiver1"); Sendbroadcast (intent1);
- Send broadcast
- Normal send
Sendbroadcast (INTENT1);
- Ordered delivery
Sendorderedbroadcast (intent1,null);
- Sequential broadcasts with broadcast registration priority use to control receiver receive order, and in high priority receivers can stop a low priority receiver to continue broadcasting
@Override Public void OnReceive (Context context, Intent Intent) { //ToDo abortbroadcast ();}
- Stranded broadcasts send Stickybroadcast, stickyorderedbroadcast
- Stranded broadcast specific performance can be sent first message, and then register to receive, that is, the first boat after the ticket
- When a stranded broadcast is sent, the system retains the information from the last stranded broadcast that was sent, and automatically receives a broadcast with that information when a receiver that meets the requirements is registered.
- The difference between Stickybroadcast and Stickyorderedbroadcast is the same as broadcast and Orderedbroadcast.
- Since it can be saved in the system, then it can be deleted, the deletion method is removestickybroadcast
- dynamic registration and static registration
- Registration in manifest is a static registration, which is automatically registered to the system after the application is installed, which consumes memory for long periods of time, and is suitable for listening to broadcasts in the system and for broadcasting that is used throughout the application lifecycle
- Android also supports a way of registering in the code, the flexibility to implement receiver registration, suitable for use in the application of internal monitoring of the message broadcast
- Dynamic registration method
- Registration
-
registerreceiver ( new
Mreceiver (), new intentfilter ("Com.example.AndroidTest_01.receiver1");
- Since there is dynamic registration there is a corresponding dynamic cancel logoff
-
mreceiver receiver=new Mreceiver ();
Unregisterreceiver (receiver);
- Dynamic logoff needs to be logged off by the instance at the time of registration, so a statically registered receiver cannot log off dynamically, and only save the instance reference on dynamic registration to log off
- registration and logoff with use to optimize application performance and reduce unnecessary resource consumption
- Sending local broadcasts and global broadcasts
- The default send broadcast message is a global broadcast, and all eligible receivers in the system are able to receive broadcast messages
- Local broadcast
Android Essentials 5:4 Large components (broadcast Receiver)