In the first article, we've seen how to use intent to launch new application components, but in fact they can also use the Sendbroadcast method to broadcast messages anonymously between components.
As a system-level messaging mechanism, intent can send structured messages between processes. Therefore, the broadcast receiver is implemented to monitor and respond to these broadcast Intent within the application.
By using intent to broadcast an event, you can let our developers react to events without modifying the original application. Android uses broadcast receiver to broadcast system events, such as network connections and SMS notifications.
1. Use intent to broadcast events
In the application component, you can build the intent that you want to broadcast, and then use the Sendbroadcast method to send it.
Intent's actions, data, and classifications can be set so that broadcast receiver can pinpoint their needs. In this scenario, the intent action string can be used to identify the event to broadcast, so he should be the only string that can identify the event. In practice, the action string is constructed in the same way as the Java package name, as follows:
<span style= "FONT-SIZE:14PX;" >public static final String my_intent_action = "Com.happy.demo.test";</span>
If you want to include data in intent, you can use the intent Data property to specify a URI, or you can include extras to add additional base values.
2. Use broadcast receiver to monitor the broadcast
Broadcast receiver can be used to listen to broadcast Intent, but it needs to be registered, can be used in code to register, You can also register in the application's Manifest.xml file, whichever way you want to use a intent filter to specify which intent and data he is listening to.
For broadcast receivers included in the Manifest.xml file, the application does not have to be in a running state to be received when intent is broadcast, and they are automatically started.
To create a new broadcast receiver, you need to extend the broadcast receiver class and override the OnReceive method to do your own processing. Like what:
<span style= "FONT-SIZE:14PX;" >package Com.happy.demo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Mybroadcastreceiver extends Broadcastreceiver {@Overridepublic void OnReceive ( Context context, Intent Intent) {//TODO auto-generated method stub}}</span>
The OnReceive method is executed when a broadcast intent that matches the intentfilter used when registering the receiver is received. The OnReceive handler must be completed within 5 seconds or the Force Close dialog box will be displayed.
In general, broadcast receiver will update the content, start the service, update the activity UI, or use Notification Manager to notify the user.
3. Registering broadcast Receiver in the code
Boradcast receiver, which affects the UI of a particular activity, is typically registered in code, and the receiver registered in the code will only broadcast Intent when the application component that contains it runs.
When the receiver is used to update a UI element in an activity, it typically registers the sink in Onresume and unregisters the sink in OnPause. Code:
<span style= "FONT-SIZE:14PX;" > @Overrideprotected void Onresume () {super.onresume (); Registerreceiver (receiver, filter);} @Overrideprotected void OnPause () {super.onpause (); Unregisterreceiver (receiver);} </span>
4. Registering in the application's Manifets.xml file
To include a broadreceiver in the application's mainfets, you can add a receiver tag to the application node and specify the class name of the broadcast receiver to be registered. The receiver node needs to include a intent-filter tag to specify the action string to listen on. As follows:
<span style= "FONT-SIZE:14PX;" ><receiver android:name= "Com.happy.demo.MyBroadcastReceiver" > <intent-filter> <action Android:name= "Com.happy.demo.one"/> </intent-filter> </receiver></span>
5. Broadcast-ordered intent
The Sendorderedbroadcast method can be used when the order in which the broadcast receiver receives the intent is important, especially if a future receiver is required to affect the broadcast intent received by the future receiver, as follows:
<span style= "FONT-SIZE:14PX;" >string requiredpermission = "Com.happy.demo.one"; Sendorderedbroadcast (intent, requiredpermission); </span >
with this method, intent will be passed to all registered receivers with appropriate permissions in order of precedence. You can use the Android:priority property in Manifest.xml to specify its permissions, and the greater the value, the higher the priority.
<span style= "FONT-SIZE:14PX;" > <receiver android:name= "com.happy.demo.MyBroadcastReceiver" android:permission= " Android.permission.ACCESS_CHECKIN_PROPERTIES "> <intent-filter android:priority=" + "> < Action android:name= "Com.happy.demo.one"/> </intent-filter> </receiver></span>
A common example of sending an ordered broadcast is a intent that broadcasts a data that wants to receive its results. When using the Sendorderedbroadcast method, you can specify a sink that will be placed at the end of the receiver queue, and from the assurance that when BROADACST receiver has been processed and modified by an ordered broadcast receiver that has already been registered, It can also receive the broadcast Intent.
In this case, it is often helpful to specify default values for intent results, data, and extra that may be modified by any receiver that receives the broadcast before returning to the last sink. As follows:
<span style= "FONT-SIZE:14PX;" >sendorderedbroadcast (Intent, Receiverpermission, Resultreceiver, Scheduler, Initialcode, Initialdata, Initialextras);</span>
6. Broadcast Stick Intent
Stity Intent is a useful variant of broadcast intent, which can hold the value of their last broadcast, and when a new receiver is registered to receive the broadcast, they return the values as intent.
When you call Registerreceiver to specify a matching Sticy broadcast Intent Intent filter, the return value will be the last intent broadcast, such as the broadcast of a battery charge change
7.Local Boradcast Manager
The local broadcast manager is included in the Android support library, where users simplify the registration of broadcast Inten and send broadcast intent between components within the program. Because the scope of the local broadcast is smaller, it is more efficient to use it than sending a global broadcast, and it is also safe to ensure that any components outside the application are not receiving our broadcast intent. In the same way, other applications cannot send broadcasts to our receivers, preventing these receivers from becoming security vulnerabilities.
Here's how to get the local broadcast manager:
<span style= "FONT-SIZE:14PX;" >localbroadcastmanager LBM = localbroadcastmanager.getinstance (Getapplicationcontext ());</span>
registering a local broadcast receiver is the same as registering a global one, with the following code:
<span style= "FONT-SIZE:14PX;" >lbm.registerreceiver (receiver, Intentfilter);</span>
to send a local broadcast Intent, you can use the Sendbroadcast method of the local Boradcast manager and pass in the Intent to broadcast. as follows:
<span style= "FONT-SIZE:14PX;" >lbm.sendbroadcast (Intent);</span>
8.Pending Intent Introduction
The Pendingintent class provides a mechanism for creating intent that can be triggered by other applications at a later time.
Pendingintent are typically used to wrap intent that are triggered when responding to future events, such as a stand-alone widget or notification.
The Pendingintent class provides a static way to build pendingintent to initiate activity service or broadcast intent.
<span style= "FONT-SIZE:14PX;" >pendingintent.getactivity (context, requestcode, intent, flags); Pendingintent.getservice (context, requestcode, intent, flags); Pendingintent.getbroadcast (context, requestcode, intent, flags);</span>
the Pendingintent class contains static constants that can be used to specify flags to update or cancel existing pendingintent that match a specified action, or to specify whether the intent fires only once.
Intent in Android (ii) the use of intent broadcast event and broadcast receiver profile