[Bloggers: Dear bloggers, netizens, and everyone else! Welcome to this blog.
You are welcome to have more exchanges, give more comments, learn from each other, and make mutual progress. Our slogan is: study hard and make progress every day .]
I have read a lot of materials recently, and I have a lot of things about widgets. Make a demo and make a summary.
Widget meaning: the basis is the meaning of the Window widget. It is some small programs on the mobile phone homepage. For example, countdown, music playback controller, and display of the current time. It is widely used.
Next, create your first widget Applet:
Create a project: widgetdemo
Widget. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: background = "@ drawable/widget" <br/> <textview <br/> Android: Id = "@ + ID/wordcup" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "@ string/Hello" <br/> Android: textsize = "12px" <br/> Android: textcolor = "# ff0000" <br/> </linearlayout>
Create Res/XML/widget_provider.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: minwidth = "60dip" Android: minheight = "60dip" <br/> Android: updateperiodmillis = "10000" Android: initiallayout = "@ layout/widget"/>
Configure androidmanifest. XML to register broadcast information:
<Cycler Android: Name = ". widgetdemo "Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. appwidget. action. appwidget_update "/> <br/> </intent-filter> <br/> <meta-data Android: Name =" android. appwidget. provider "Android: Resource =" @ XML/widget_provider "/> <br/> </javaser>
Create widgetdemo. Java to process the business logic:
Import Java. util. calendar; <br/> Import Java. util. date; <br/> Import Java. util. gregoriancalendar; <br/> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import android. appwidget. appwidgetmanager; <br/> Import android. appwidget. appwidgetprovider; <br/> Import android. content. componentname; <br/> Import android. content. context; <br/> Import android. widget. remoteviews; <br/> public class widgetdemo extends appwidgetprovider {</P> <p>/** <br/> * update widgets <br/> */<br/> @ override <br/> Public void onupdate (context, appwidgetmanager, int [] appwidgetids) {<br/> timer = new timer (); <br/> timer. scheduleatfixedrate (New atime (context, appwidgetmanager), 1, 60000); <br/> super. onupdate (context, appwidgetmanager, appwidgetids ); <br/>}</P> <p>/** <br/> * timer implementation <br/> */<br/> private class atime extends timertask {<br/> remoteviews; <br/> appwidgetmanager; <br/> componentname thiswidget; </P> <p> date = new date (); <br/> calendar = new gregoriancalendar (, 13); <br/> long days = (calendar. gettimeinmillis ()-date. gettime ()/1000)/86400; </P> <p> Public atime (context, appwidgetmanager) {<br/> This. appwidgetmanager = appwidgetmanager; <br/> remoteviews = new remoteviews (context. getpackagename (), R. layout. widget1); </P> <p> thiswidget = new componentname (context, widgetdemo. class); <br/>}< br/> Public void run () {<br/> remoteviews. settextviewtext (R. id. wordcup, "+ days +" days "); <br/> appwidgetmanager. updateappwidget (thiswidget, remoteviews); <br/>}< br/>}
Final effect:
On the main page of the mobile phone, press and hold on, click the widget, and select wdigetdemo, OK.
The introduction is complete. I hope to help you.
Indicate the source for reprinting.