Android Basics (ii) Broadcast receiver knowledge

Source: Internet
Author: User

1. Registration of Broadcasts

(1) Dynamic broadcast registration:

Advantages: Can flexibly control the registration and revocation of broadcasting

Disadvantage: You must be able to receive broadcasts when the program starts

Intentfilter: When intent is passed between builds, the component wants to tell the Android system that it can respond to and handle those intent, using the Intentfilter class, The Intentfilter object is responsible for filtering out the intent that the component cannot respond to and handling, and only receives the intent that it cares about.

Intentfilter.addaction (): Intent,intentfilter not set action according to Action settings, all intent cannot pass. Intent does not set the action to pass all the intentfilter.

Broadcastreceiver Subclass: Receives the intent passed by the Intentfilter, and distinguishes intent according to the action of the intent, performing the corresponding action in the overridden OnReceive () method.

Dynamic Registration/Revocation: Call Registerreceiver (Broadcastreceiver br,intentfilter if) to register the broadcast;

Call Unregisterreceiver (Broadcastreceiver br,intentfilter if) to unregister the broadcast.

(2) Static broadcast registration:

Add <receiver> tags to androidmanifest.xml

<receiverAndroid:name= "Com.feng.Reciver"><!--Custom Broadcastreceiver sub-class -            <Intent-filter>                <ActionAndroid:name= "Com.android.broadcasttest.NEW_LIFEFROM"/><!--the intent action--> that the broadcast receiver can filter
</Intent-filter>
</receiver>

Broadcastreceiver is not allowed to open a sub-thread, so broadcastreceiver more is used for opening other components, such as the opening of a service, send a notification bar message, and so on.

2. Send Custom Broadcasts

Intent Intent =new Intent (Intent action name)//The name of the Intent action to be added for the broadcast receiver when registering the broadcast receiver, if you want to

You can specify intent's action name at this time

Sendbroadcast (Intent)//can also be processed by intent carrying some data to Broadcastreceiver onreceive ()

3. Send an orderly broadcast

Intent Intent =new Intent (Intent action name);

Sendorderedbroadcast (intent, NULL);//The second parameter is a permission-related string, which is empty

The broadcasts above are all global broadcasts, and the global broadcast is a cross-process , that is, if the action of the corresponding intent is registered in the broadcast receiver, either the broadcast issued by the application or the broadcast of the other application will only carry the intent of the action, Receiver can be received.

It is obvious that such a global broadcast is a security risk, the application will be sent to broadcast if other applications have the means to receive, then there is a risk of sensitive data leakage, and the application will be received from other applications broadcast, if these broadcasts are spam, Then the application is at risk of being attacked.

So on the basis of the global broadcast, the Android system proposes another more secure broadcast delivery acceptance mechanism--local broadcast.

4. Local broadcast :

Broadcasts issued using this mechanism can only receive delivery within the application, and the broadcast receiver can only receive broadcasts from the application, so that all security problems are absent.

Usage: Mainly uses the Localbroadcastmanager class to manage the broadcast

Other principles of broadcast transmission and registration are the same, except that they are sent and registered using the Localbroadcast class object when the broadcast is sent or registered.

//send a local broadcastLocalbroadcastmanager localbroadcastmanager=localbroadcastmanager.getinstance (); Intent Intent=NewIntent (Intent action name); Localbroadcastmanager.sendbroadcast (Intent);//Register for local broadcastIntentfilter intentfilter=NewIntentfilter (); Intentfilter.setaction (Intent action name); Localbroadcastmanager.registerreceiver (Newlocalreceiver (), intentfilter);//revoke a local broadcastLocalbroadcastmanager.unregisterreceiver (Newlocalreceiver (), intentfilter);classLocalreceiverextendsbroadcastreceiver{@override Public voidOnreceiver (Context context, Intent Intent) {//TODO auto-generated Method Stub}}

Android Basics (ii) Broadcast receiver knowledge

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.