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