Android Widget简单应用之奥运会倒计时


Android Widget桌面小部件是可以在主页上显示并频繁更新的视图。作为视图,部件的观感通过布局xml文件来定义。对于部件,除了视图的布局,还需要定义部件视图将需要在屏幕上占用多大空间。

部件视图还包括一对Java类,他们负责初始化视图并频繁更新它,这些Java类负责在主屏幕上管理部件的生命周期。当将部件拖到屏幕上,以及将部件拖到回收站进行卸载时,这些类进行相应。

下面通过一个伦敦奥运会倒计时的简单Widget例子来说明如何创建一个桌面小部件。
1、定义一个AppWidgetProviderInfo,在/res/xml/widget.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <appwidget-provider  
  3.       xmlns:android="http://schemas.android.com/apk/res/android"  
  4.       android:minWidth="50dip"    
  5.     android:minHeight="50dip"    
  6.     android:updatePeriodMillis="10000"    
  7.     android:initialLayout="@layout/main"  
  8.   >  
  9. </appwidget-provider>  
2、为AppWidget指定样式和布局:main.xml
  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/icon"  
  7.     >  
  8. <TextView      
  9.     android:id="@+id/Olympic"    
  10.     android:layout_width="fill_parent"     
  11.     android:layout_height="wrap_content"     
  12.     android:text="@string/hello"    
  13.     android:textSize="12px"    
  14.     android:textColor="#ff00ff"    
  15.     />  
  16. </LinearLayout>  
3、实现AppWidgetProvider类
  1. public class WidgetDemo extends AppWidgetProvider {  
  2.     @Override  
  3.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  4.             int[] appWidgetIds) {  
  5.         // TODO Auto-generated method stub   
  6.         Timer timer = new Timer();    
  7.         timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 160000);    
  8.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  9.     }  
  10.     //定时任务类   
  11.     private class MyTime extends TimerTask{    
  12.         RemoteViews remoteViews;    
  13.         AppWidgetManager appWidgetManager;    
  14.         ComponentName thisWidget;    
  15.             
  16.         public MyTime(Context context,AppWidgetManager appWidgetManager){    
  17.             this.appWidgetManager = appWidgetManager;    
  18.             remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);    
  19.                 
  20.             thisWidget = new ComponentName(context,WidgetDemo.class);    
  21.         }    
  22.         //定时任务内容   
  23.         public void run() {    
  24.             Date date = new Date();    
  25.             Calendar calendar = new GregorianCalendar(2012,06,28);    
  26.             long days = ((calendar.getTimeInMillis()-date.getTime())/(1000*60*60*24));    
  27.             remoteViews.setTextViewText(R.id.Olympic, "距离伦敦奥运会还有" + days+"天");  
  28.             //更新Widget内容   
  29.             appWidgetManager.updateAppWidget(thisWidget, remoteViews);    
  30.         }    
  31.     }  
  32. }  
4、在AndroidManifest.xml中注册:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.test.widget"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.   
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.         <receiver android:name=".WidgetDemo"  
  9.                   android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
  12.             </intent-filter>  
  13.             <meta-data android:name="android.appwidget.provider"    
  14.                        android:resource="@xml/widget"    
  15.             />  
  16.         </receiver>  
  17.     </application>  
  18. </manifest>  

运行程序,长按桌面在谈出的选项中选择窗口小部件,结果如图:



然后选择olympic widget,结果如图所示:

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容