Android research-Development of Android widget Basics

Source: Internet
Author: User

Reference address: http://mobile.51cto.com/widget-290079.htm

 

Ndroid widget DevelopmentCase implementation is the content to be introduced in this article, mainly to understand and learnAndroid widget DevelopmentApplication. Let's write about it today.Android widgetOfDevelopmentBecause it's time to wait early in the morning, I won't say it's too specific. Let's just imitate it, comrades! 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:

  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Resources>
  3. <String Name="Hello">Hello world, widetdemo!</String>
  4. <String Name="App_name">Daystoworldcup</String>
  5. </Resources>

Iv. EstablishmentWidgetContent Provider file. We create an XML folder under Res and create a new widget_provider.xmlCodeEntry:

  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Appwidget-provider Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Android: minwidth="50dip"
  4. Android: minheight="50dip"
  5. Android: updateperiodmillis="10000"
  6. Android: initiallayout="@ Layout/main"
  7. />

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

  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Linearlayout Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Android: Orientation="Vertical"
  4. Android: layout_width="Fill_parent"
  5. Android: layout_height="Fill_parent"
  6. Android: Background="@ Drawable/wordcup"
  7. >
  8. <Textview
  9. Android: ID="@ + ID/wordcup"
  10. Android: layout_width="Fill_parent"
  11. Android: layout_height="Wrap_content"
  12. Android: Text="@ String/hello"
  13. Android: textsize="12px"
  14. Android: textcolor="# Ff0000"
  15. />
  16. </Linearlayout>

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

  1. Package com. Android. tutor;
  2. Import java. util. calendar;
  3. Import java. util. date;
  4. Import java. util. gregoriancalendar;
  5. Import java. util. timer;
  6. Import java. util. timertask;
  7. Import Android. appwidget. appwidgetmanager;
  8. Import Android. appwidget. appwidgetprovider;
  9. Import Android. content. componentname;
  10. Import Android. content. context;
  11. Import Android. widget. remoteviews;
  12. Public class widetdemo extends appwidgetprovider {
  13. /** Called when the activity is first created .*/
  14. @ Override
  15. Public void onupdate (context, appwidgetmanager,
  16. Int [] appwidgetids ){
  17. TimerTimer=NewTimer ();
  18. Timer. scheduleatfixedrate (New mytime (context, appwidgetmanager), 1, 60000 );
  19. Super. onupdate (context, appwidgetmanager, appwidgetids );
  20. }
  21. Private class mytime extends timertask {
  22. Remoteviews;
  23. Appwidgetmanager;
  24. Componentname thiswidget;
  25. Public mytime (context, appwidgetmanager ){
  26. This. appwidgetmanager= Appwidgetmanager;
  27. Remoteviews=NewRemoteviews (context. getpackagename (), R. layout. Main );
  28. Thiswidget=NewComponentname (context, widetdemo. Class );
  29. }
  30. Public void run (){
  31. DateDate=NewDate ();
  32. CalendarCalendar=NewGregoriancalendar (2010,06, 11 );
  33. LongDays= (Calendar. gettimeinmillis ()-date. gettime ()/1000)/86400;
  34. Remoteviews. settextviewtext (R. Id. wordcup, "+ days +" days "from South Africa World Cup ");
  35. Appwidgetmanager. updateappwidget (thiswidget, remoteviews );
  36. }
  37. }
  38. }

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

  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Manifest Xmlns: Android=Http://schemas.android.com/apk/res/android"
  3. Package="Com. Android. Tutor"
  4. Android: versioncode="1"
  5. Android: versionname="1.0">
  6. <Application Android: icon="@ Drawable/icon" Android: Label="@ String/app_name">
  7. <Cycler Android: Name=". Widetdemo"
  8. Android: Label="@ String/app_name">
  9. <Intent-Filter>
  10. <Action Android: Name="Android. appwidget. Action. appwidget_update" />
  11. </Intent-Filter>
  12. <Meta-data Android: Name="Android. appwidget. provider"
  13. Android: Resource="@ XML/widget_provider"
  14. />
  15. </Cycler>
  16. </Application>
  17. <Uses-SDK Android: minsdkversion="7" />
  18. </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:

 

Summary: AnalysisAndroid widget DevelopmentThe implementation of the case is complete.Android widget DevelopmentContent learning is helpful to you!

 

This article is complete.

Related Article

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.