Component development of desktop clocks:
The entire process is as follows:
Demo:www.github.com/xufeifandj
Service+ component + Broadcast background real time update time
(a) The boot-up broadcast monitoring starts after the service is started. Register a broadcast immediately to change the listening time.
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (Intent.action_time_tick); Time has changed.
Intentfilter.addaction (intent.action_time_changed); //
Intentfilter.addaction (intent.action_date_changed); Date changes
Intentfilter.addaction (intent.action_timezone_changed); Time zone changes
This.getbasecontext (). Registerreceiver (Widgetsupdatereceiver, Intentfilter);
(ii) When the broadcast supervisor hears that time has changed, it can update the component
Class Updatereceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
Updatewidget ();
}
}
Send a broadcast to update the component to
Public synchronized void Updatewidget () {
Sendbroadcast (New Intent ("Com.android.FERRIS_UPDATE_WIDGET"));
}
(3) Weatherwidget_time inherited from Appwidgetprovider is actually a broadcast.
We are here to register some updated action
When a component changes. To update
public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,
Int[] appwidgetids) {
for (int i = 0; i < appwidgetids.length; i++) {
Remoteviews Updateview = lewabuildupdate (context, appwidgetids[i],false,true,true);
Appwidgetmanager.updateappwidget (Appwidgetids[i], updateview);
}
Super.onupdate (context, Appwidgetmanager, appwidgetids);
}
@Override
public void OnReceive (final context context, Intent Intent) {
if (Intent.getaction (). Equals (intent.action_boot_completed)) {//boot start
Start the time background service. Dynamic go-to-register broadcast
Intent service=new Intent (context, widgettimeservice.class);
Context.startservice (service);
Updatetimealarm (context); and go and update the clock.
}else if (intent.getaction (). Equals ("Com.android.FERRIS_UPDATE_WIDGET")) {//Supervisor hears time changes. Update Wiget
Updatetimealarm (context);
}
Super.onreceive (context, intent);
}
(4) Update time
Public synchronized void Updatetimealarm (context context) {
// Appwidgetmanager Appwidgetmanger = Appwidgetmanager
. getinstance (context);
int[] Appids = appwidgetmanger.getappwidgetids (New ComponentName (
Context, Weatherwidgetv5_time.class));
//
ComponentName Provider = new ComponentName (context,context.getpackagename () + ". Weatherwidget_time ");
Appwidgetmanager gm = appwidgetmanager.getinstance (context);
Int[] Appwidgetids;
Appwidgetids = Gm.getappwidgetids (provider);
for (int i = 0; i < appwidgetids.length; i++) {//Traversal for update
Remoteviews Updateview = lewabuildupdate (context, appwidgetids[i],false,true,true); Updates the UI component. The parameters are the date of the week, and so on. is updated
Gm.updateappwidget (Appwidgetids[i], updateview);
}
}
"Weather app" desktop clock Witget component