Component development of desktop clocks:
The entire process is as follows:
Service+ component + Broadcast background real time update time
(a) the start-Up Broadcast monitoring boot service starts, the dynamic registration of a broadcast to listen to the change in 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 modification
Intentfilter.addaction (intent.action_timezone_changed); Time Zone Modification
This.getbasecontext (). Registerreceiver (Widgetsupdatereceiver, Intentfilter);
(ii) The broadcast supervisor hears that there is a time change and can update the component
Class Updatereceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
Updatewidget ();
}
}
Send a broadcast to update the component
Public synchronized void Updatewidget () {
Sendbroadcast (New Intent ("Com.android.FERRIS_UPDATE_WIDGET"));
}
(3) Weatherwidget_time inherited from Appwidgetprovider is actually a broadcast. We have registered some of the updated action here
Update when the component is changed
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 time background service, dynamic to register the 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")) {//Monitor to hear 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); Update the UI component with the parameters for the date week, and so on. is updated
Gm.updateappwidget (Appwidgetids[i], updateview);
}
}
"Weather app" desktop clock Witget component