Android高手進階教程(八)之----Android Widget開發案例(世界盃倒計時!)

今天我們要寫一下Android Widget的開發,由於快點凌晨,我就不說的太具體了,同志們就模仿吧!首先看一下效果圖:
 
下面是Demo的詳細步驟:
一、新建一個Android工程命名爲:WidgetDemo.
 
二、準備素材,一個是Widget的圖標,一個是Widget的背景。存放目錄如下圖:
 
三、修改string.xml文件如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <resources>     
  4.     <string name="hello">Hello World, WidetDemo!</string>     
  5.     <string name="app_name">DaysToWorldCup</string>     
  6. </resources>  
 
四、建立Widget內容提供者文件,我們在res下建立xml文件夾,並且新建一個widget_provider.xml代碼入下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <appwidget-provider 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. />       
五、修改main.xml佈局,代碼如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:orientation="vertical"    
  5.     android:layout_width="fill_parent"    
  6.     android:layout_height="fill_parent"    
  7.     android:background="@drawable/wordcup"    
  8.     >     
  9. <TextView       
  10.     android:id="@+id/wordcup"    
  11.     android:layout_width="fill_parent"      
  12.     android:layout_height="wrap_content"      
  13.     android:text="@string/hello"    
  14.     android:textSize="12px"    
  15.     android:textColor="#ff0000"    
  16.     />     
  17. </LinearLayout>    
  18.    
  19. 六、修改WidgetDemo.java代碼如下:  
  20. view plaincopy to clipboardprint?  
  21. package com.android.tutor;     
  22. import java.util.Calendar;     
  23. import java.util.Date;     
  24. import java.util.GregorianCalendar;     
  25. import java.util.Timer;     
  26. import java.util.TimerTask;     
  27. import android.appwidget.AppWidgetManager;     
  28. import android.appwidget.AppWidgetProvider;     
  29. import android.content.ComponentName;     
  30. import android.content.Context;     
  31. import android.widget.RemoteViews;     
  32. public class WidetDemo extends AppWidgetProvider {     
  33.     /** Called when the activity is first created. */    
  34.        
  35.     @Override    
  36.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,     
  37.             int[] appWidgetIds) {     
  38.              
  39.         Timer timer = new Timer();     
  40.         timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 160000);     
  41.         super.onUpdate(context, appWidgetManager, appWidgetIds);     
  42.     }     
  43.          
  44.          
  45.     private class MyTime extends TimerTask{     
  46.         RemoteViews remoteViews;     
  47.         AppWidgetManager appWidgetManager;     
  48.         ComponentName thisWidget;     
  49.              
  50.         public MyTime(Context context,AppWidgetManager appWidgetManager){     
  51.             this.appWidgetManager = appWidgetManager;     
  52.             remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);     
  53.                  
  54.             thisWidget = new ComponentName(context,WidetDemo.class);     
  55.         }     
  56.         public void run() {     
  57.                  
  58.             Date date = new Date();     
  59.             Calendar calendar = new GregorianCalendar(2010,06,11);     
  60.             long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;     
  61.             remoteViews.setTextViewText(R.id.wordcup, "距離南非世界盃還有" + days+"天");     
  62.             appWidgetManager.updateAppWidget(thisWidget, remoteViews);     
  63.                  
  64.         }     
  65.              
  66.     }     
  67.        
  68. }    
七、修改配置文件AndroidManifest.xml,代碼如下:
  1. view plaincopy to clipboardprint?  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?> 
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  5.       package="com.android.tutor" 
  6.       android:versionCode="1" 
  7.       android:versionName="1.0"> 
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <receiver android:name=".WidetDemo" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
  13.             </intent-filter> 
  14.             <meta-data android:name="android.appwidget.provider" 
  15.                  android:resource="@xml/widget_provider" 
  16.             /> 
  17.         </receiver> 
  18.     </application> 
  19.     <uses-sdk android:minSdkVersion="7" /> 
  20. </manifest>    
八、點擊運行(Ctrl+F11),之,運行成功後,我們長時間點擊桌面,會出現如下倆個,依次點擊,就可以看到最上面的效果圖:
 
今天就到這裏了,我困了呵呵,我發現時間好像不對勁,lol~我也不去多想了,大家知道的告訴我下!對日曆這些東西不是太瞭解,謝謝!!
 
 

本文出自 “Android_Tutor” 博客,請務必保留此出處http://weizhulin.blog.51cto.com/1556324/311438


發佈了14 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章