Android (Eclipse) broadcast mechanism knowledge Grooming (iii)

Source: Internet
Author: User

1: Category:
Standard broadcast: No sequencing, cannot be truncated
Orderly broadcast: sequential, can truncate

2: Receive broadcast: First to register, the way of registration has static registration and dynamic registration. That is, registering in code and registering in Androidmanifest, and declaring permissions in manifest, whether static or dynamic, when it comes to system-critical information:

<users-permission andorid:name= "Android.permission.ACCESS_NETWORK_STATE"/> Querying network status

<users-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/> Accept system Broadcast

<uers-permisson android:name= "Android.permisson.SYSTEM_ALERT_WINDOW"/> Popup System-Level dialog box

  

<user-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <receiver android:name=". Bootcmpletereceiver">        <intent-filter>            <action android:name="  Android:intent.action.BOOT_COMPLETED"/></intent-filter></recerver>

Send a standard broadcast

 Publiccalss Mybroadcastreceiver extends broadcastreceiver{@override Public voidonreceive (Context context,intent Intent) {toast.maketext (context,"*****", Toast.length_short). Show (); }}<manifest Xmlns:andorid=........><application.....><receiver android:name=". Mybroadcastreceiver"> <intent-filter> <action andrid:name="Com.example.broadcasttest.MYBROADCAST"/></receiver></application></manifest>Mainactivity:button Button=(Button) Findviewbyid (r.id.   Send); Burron.setonclicklistener (NewOnclicklistener () {@override Public voidOnClick (View v) {Intent Inten=NewIntent ("Sign up for a good signal when registering"); Sendbroadcast (intent);//sendorderbroadcast (Intent,null) sends an ordered broadcast}});

Send an ordered broadcast:
Need to include <intent-filter android:priority= "* *" > Priority representation statement in the declaration file

If you do not wish to continue the transmission, then join in the broadcast event: Abortbroadcast ();

3: Use of local broadcast

  

public class mainactivity Extens activity{private Intentfilter intentfilter;    Private Localbroadcastmanager Localbroadcastmanager;    Private Localreceiver Localreceiver; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout ...); Button button= (button) Findviewbyid (r.id.send); localbroadcastmanager= localbroadcastmanager.gerinstance (this);// Gets the instance Button.setonclicklister (new Onclicklistener () {@overridepublic void OnClick (View v) {Intent intent=new Intent ("" Com.example.broadcast.MYBROADCAST); Localbroadcastmanager.        Sendbroadcast (intent);}  });        Intentfilter=new Intentfilter ();        Localreceiver=new Localreceiver ();        Intentfilter.addaction ("Com.example.broadcast.MYBROADCAST");    Localbroadcastmanager.registerreceiver (Localreceiver,intentfilter);   } @Override protected void OnDestroy () {Super.ondestroy ();  Localbroadcastmanager.unregisterreceiver (Localreceiver); } @Override Class LocalrEceiver () {public void OnReceive () {...}} 

Local broadcast for security can not be through static registration, general static registration is actually implemented when the boot start will be used to say that the program is not open before you will receive the broadcast.

Note: Be sure to use Intent.addflags (intenten.flag_activity_task) when initiating activity in the broadcast receiver.

And if you just call the static method of the class then you can not instantiate it, static method is not a tired object, belongs to the class.

 

Android (Eclipse) broadcast mechanism knowledge Grooming (iii)

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.