"Weather app" desktop clock Witget component

Source: Internet
Author: User

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

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.