"Weather app" desktop clock Witget component

Source: Internet
Author: User

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

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.