Android-broadcast static Dynamic Broadcast

Source: Internet
Author: User

Static and dynamic types of broadcasts

Static broadcast:

1. Broadcastreceiver of succession

 Public classMystaticbroadcastreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.widget_layout);Toast.maketext (Context,"Zcxczcxstatic", Toast.length_short). Show (); LOG.D ("Zcx", "Staticbroad"); //Remoteviews.settextviewtext (R.id.widget_text, Intent.getstringextra ("MESSAGE")); //context.startactivity (intent);Alertdialog.builder Builder =NewAlertdialog.builder (context); Builder.settitle (Prompted). Setmessage ("Receive broadcasts for Broadcastsend applications"). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {}}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)        {                    }                }); Alertdialog Dialog=(Alertdialog) builder.create ();        Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);                Dialog.show (); //appwidgetmanager.getinstance (context). Updateappwidget (New ComponentName (//Context.getapplicationcontext (), Mywidgetprovider.class), remoteviews);}

2. Defined in Androidmanifest

<receiverandroid:priority= "+"Android:name=". Mystaticbroadcastreceiver "android:permission= "Com.example.zcx.permission"android:process= ": Remote"            >            <Intent-filter>                <ActionAndroid:name= "Android.zcx"></Action>            </Intent-filter></receiver>>

3.client Call

LOG.D ("Zcx", "Sendstaticbroadcastbutton");                 // Toast.maketext (Getapplicationcontext (), "Zcxczcx", Toast.length_short). Show ();                New Intent ();                Intent.setaction ("android.zcx");                 // intent.setaction ("android.appwidget.action.zcx");                 intent.addflags (flag_include_stopped_packages);                 // Intent.putextra ("MESSAGE", Edittext.gettext (). toString ());                Sendbroadcast (Intent);

Dynamic Broadcast:

1. Broadcastreceiver of succession

@Override Public voidOnReceive (Context context, Intent Intent) {//remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.widget_layout);LOG.D ("Zcx", "Mydynamicbroadcastreceiver"); //Remoteviews.settextviewtext (R.id.widget_text, Intent.getstringextra ("MESSAGE"));Toast.maketext (Context, "zcxczcxdynamic", Toast.length_short). Show (); //appwidgetmanager.getinstance (context). Updateappwidget (New ComponentName (//Context.getapplicationcontext (), Mywidgetprovider.class), remoteviews);Alertdialog.builder Builder =NewAlertdialog.builder (context); Builder.settitle (Prompted). Setmessage ("Received broadcast of the Broadcastsend dynamic application"). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {}}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)        {                    }                }); Alertdialog Dialog=(Alertdialog) builder.create ();        Dialog.getwindow (). SetType (WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);    Dialog.show (); }

2. Service-side code registration starts

New Intentfilter ();        Dynamic_filter.addaction ("android.appwidget.action.zcx");         New mydynamicbroadcastreceiver ();        Registerreceiver (mydynamicbroadcastreceiver,dynamic_filter);

3. Client Calls

 Public void OnClick (View v) {                new  Intent ();                 // intent.setaction ("android.zcx");                Intent.setaction ("Android.appwidget.action.zcx");                 // intent.addflags (flag_include_stopped_packages);                 // Intent.putextra ("MESSAGE", Edittext.gettext (). toString ());                 Sendbroadcast (intent);}

The difference between dynamic broadcast and static broadcast:

(1) Dynamic registration broadcasts are not resident broadcasts, which means that the broadcast follows the activity's life cycle. Note To remove the broadcast receiver before the activity ends.

Static registration is permanent, that is, when the application is closed, if there is information broadcast, the program will be automatically run by the system call.

Here the use of dynamic and static implementation, service and client, service to open, the customer can invoke the service broadcast receive, dynamic can understand, static why?

Android-broadcast static Dynamic Broadcast

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.