Android Intent specific Explanation (ii) use of intent broadcast event and broadcast receiver brief introduction

Source: Internet
Author: User

With the first commentary, 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 is able to 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 do so without altering the original application. Let us developers react to events. 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 such a 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. Habits. The action string is built in the same way as the Java package name. For example, the following:

<span style= "FONT-SIZE:14PX;" >public static final String my_intent_action = "Com.happy.demo.test";</span>

Suppose 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 monitor broadcast Intent, but it needs to be registered. You can use code to register. You can also register in the application's Manifest.xml file. Either way, you need to use a intent filter to specify which intent and data he is listening to.

For broadcast receivers included in the Manifest.xml file. When the intent was broadcast out. The application does not have to be in the execution state to receive. They will be activated on their own initiative.

To create a new broadcast receiver, you need to extend the broadcast receiver class and override the OnReceive method to do your own processing. Example:

<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 run when a broadcast intent that matches the intentfilter used in the register receiver is received. The OnReceive handler must be completed within 5 seconds. Otherwise, the Force Close dialog box is displayed.

Under normal circumstances, broadcast receiver will update the content, start the service, update the activity UI, or use Notification Manager to notify the user.

3. Register broadcast Receiver in the code

Boradcast receiver, which affects the UI of a particular activity, is usually registered in the code, and the receiver that is registered in the code will only correspond to broadcast Intent when the application component that includes it executes.

When the receiver is used to update a UI element in an activity, the receiver is typically registered in the Onresume, and the receiver is unregistered 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. Register in the application's Manifets.xml file

To include a broadreceiver in the mainfets of the application. The ability to add a receiver tag to the application node. And specifying 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. For example, the following:

<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

When broadcast receiver receives the order of the intent is very important. In particular, when a future receiver is required to affect the broadcast intent received by a future receiver. You can use the Sendorderedbroadcast method. For example, the following:

<span style= "FONT-SIZE:14PX;" >string requiredpermission = "Com.happy.demo.one"; Sendorderedbroadcast (intent, requiredpermission); </span >

when using this method. The intent will be delivered to all registered receivers with appropriate permissions in order of priority. The ability to use the Android:priority property in Manifest.xml to specify its permissions, the greater the value. The higher the delegate 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 the intent that broadcasts the data that wants to receive its results. When using the Sendorderedbroadcast method, it is possible to specify a receiver that will be placed at the end of the receiver queue, and from the assurance that when BROADACST receiver has been processed and altered by an ordered broadcast receiver that has been registered, It can also receive the broadcast Intent.

In such a case. It is often helpful to assign default values to intent results, data, and extra that may be altered by the receiver of the broadcast before returning to the last receiver. For example, the following:

<span style= "FONT-SIZE:14PX;" >sendorderedbroadcast (Intent, Receiverpermission, Resultreceiver, Scheduler, Initialcode, Initialdata, Initialextras);</span>
6. Broadcast Stick Intent

Stity Intent is a utility variant of broadcast intent, able to hold the values 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 a broadcast of a battery charge change

7.Local Boradcast Manager

The local broadcast manager is included in the Android support library. Users simplify the manual 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 no component outside of the application is receiving our broadcast intent. The same with it. Other applications also cannot send broadcasts to our receivers. These receivers are prevented from becoming a security vulnerability.

The methods for obtaining the local broadcast manager are as follows:

<span style= "FONT-SIZE:14PX;" >localbroadcastmanager LBM = localbroadcastmanager.getinstance (Getapplicationcontext ());</span>
Register a local broadcast receiver and register the same as the global, code such as the following:

<span style= "FONT-SIZE:14PX;" >lbm.registerreceiver (receiver, Intentfilter);</span>
to send a local broadcast Intent, you can use the local Boradcast manager's Sendbroadcast method and pass in the Intent to broadcast. For example, the following:

<span style= "FONT-SIZE:14PX;" >lbm.sendbroadcast (Intent);</span>

8.Pending Intent Brief Introduction

The Pendingintent class provides a mechanism for creating intent that can be triggered by other applications at a later time.

Pendingintent is often used to package intent that are triggered when responding to future events, such as stand-alone widgets 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 includes static constants that can be used to specify flags to update or cancel an existing pendingintent that matches a specified action, or to specify whether the intent fires only once.


Android Intent specific Explanation (ii) use of intent broadcast event and broadcast receiver brief introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.