Build widgets on the Android platform:
1. Create the layout file of the widget under Res/layout: digitalclock. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Textview
Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: ID = "@ + ID/time"
Android: textsize = "45px"
Android: scrollx = "30px"
Android: scrolly = "30px"
Android: textstyle = "Bold"
Android: textcolor = "# Ff000000"
Android: Background = "@ Drawable/BG"
/>
2. Create the description file of the widget in RES/xml: est_appwidget.xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Appwidget-provider Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: minwidth = "186dip"
Android: minheight = "212dip"
Android: updateperiodmillis = "1000"
Android: initiallayout = "@ Layout/digitalclock" />
3. inherit a class (esttime) from appwidgetprovider and override its virtual Method
Code
Package Com. Android. Test. esttime;
Import Android. App. Service;
Import Android. appwidget. appwidgetmanager;
Import Android. appwidget. appwidgetprovider;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. Text. format. time;
Import Android. widget. remoteviews;
Public Class Esttime Extends Appwidgetprovider {
/** Called when the activity is first created. */
@ Override
Public Void Onupdate (context,
Appwidgetmanager, Int [] Appwidgetids)
{
Context. startservice ( New Intent (context, UpdateService. Class ));
}
// Service
Public Static Class UpdateService Extends Service {
@ Override
Public Void Onstart (intent, Int Startid ){
Time esttime = New Time ( " EST " );
Esttime. settonow ();
Remoteviews updateviews =
New Remoteviews (getpackagename (),
R. layout. digitalclock );
Updateviews. settextviewtext (R. Id. Time, esttime. Format ( " % H: % m " ));
Componentname thiswidget = New Componentname ( This , Esttime. Class );
Appwidgetmanager Manager = Appwidgetmanager. getinstance ( This );
manager. updateappwidget (thiswidget, updateviews);
}
@ Override
PublicIbinder onbind (intent ){
//Todo auto-generated method stub
Return Null;
}
}
}
4. Register this widget in androidmanifest. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Manifest Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Package = "Com. Android. Test. esttime"
Android: versioncode = "1"
Android: versionname = "1.0" >
< Application Android: icon = "@ Drawable/icon" Android: Label = "@ String/app_name" >
< Cycler Android: Name = ". Esttime"
Android: Label = "@ String/app_name" >
< Intent-Filter >
< Action Android: Name = "Android. appwidget. Action. appwidget_update" />
</ Intent-Filter >
< Meta-data Android: Name = "Android. appwidget. provider"
Android: Resource = "@ XML/est_appwidget" />
</ Cycler >
< Service Android: Name = ". Esttime $ UpdateService" />
</ Application >
< Uses-SDK Android: minsdkversion = "3" />
</ Manifest >
Finally, compile and run. In this way, the newly created weiget is displayed in the widget list popped up by long-pressing on the home.