Android advanced tutorial (8)-android widget development case (countdown to the World Cup !)

Source: Internet
Author: User

Today, let's write about the development of Android Widgets. As soon as we get up early in the morning, I will not talk about the specifics. Let's just imitate them! First, let's take a look:

 

 

The detailed steps of the demo are as follows:

 

1. Create an android project named widgetdemo.

 

2. Prepare materials. One is the widget icon and the other is the background of the widget. Storage directory:

 

 

3. Modify the string. xml file as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <string name = "hello"> Hello world, widetdemo! </String> <br/> <string name = "app_name"> daystoworldcup </string> <br/> </resources> <br/>

 

4. Create a widget content provider file. Create an XML folder under Res and create a new widget_provider.xml code:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <appwidget-provider xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: minwidth = "50dip" <br/> Android: minheight = "50dip" <br/> Android: updateperiodmillis = "10000" <br/> Android: initiallayout = "@ layout/main" <br/>

 

5. Modify the main. xml layout. The Code is as follows:

 

<? 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/wordcup" <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> <br/>

 

6. modify the code of widgetdemo. Java as follows:

Package COM. android. tutor; <br/> 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 widetdemo extends appwidgetprovider {<br/>/** called when the activity is first created. */</P> <p> @ override <br/> Public void onupdate (context, appwidgetmanager, <br/> int [] appwidgetids) {</P> <p> timer = new timer (); <br/> timer. scheduleatfixedrate (New mytime (context, appwidgetmanager), 1, 60000); <br/> super. onupdate (context, appwidgetmanager, appwidgetids); <br/>}</P> <p> private class mytime extends timertask {<br/> remoteviews; <br/> appwidgetmanager; <br/> componentname thiswidget; </P> <p> Public mytime (context, appwidgetmanager) {<br/> This. appwidgetmanager = appwidgetmanager; <br/> remoteviews = new remoteviews (context. getpackagename (), R. layout. main); </P> <p> thiswidget = new componentname (context, widetdemo. class); <br/>}< br/> Public void run () {</P> <p> date = new date (); <br/> calendar = new gregoriancalendar (2010,06, 11); <br/> long days = (calendar. gettimeinmillis ()-date. gettime ()/1000)/86400; <br/> remoteviews. settextviewtext (R. id. wordcup, "+ days +" days "); <br/> appwidgetmanager. updateappwidget (thiswidget, remoteviews); </P> <p >}</P> <p>}

 

7. modify the configuration file androidmanifest. xml. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. tutor "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <Cycler Android: Name = ". widetdemo "<br/> 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 "<br/> Android: resource = "@ XML/widget_provider" <br/> </javaser> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> </manifest>

 

8. Click "run" (CTRL + F11). After the operation is successful, we click "desktop" for a long time. The following two items are displayed. Click them one by one to see the top:

 

 

Today I am here. I am sleepy. I found that the time seems to be wrong, lol ~ I don't think much about it either. Let me know what you know! I don't know much about calendar. Thank you !!

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.