Android Widget 实例(世界杯倒计时)

来自:http://blog.csdn.net/android_tutor/article/details/5544471#


今天我们要写一下Android Widget的开发,由于快点凌晨,我就不说的太具体了,同志们就模仿吧!首先看一下效果图:

 

 

下面是Demo的详细步骤:

 

一、新建一个Android工程命名为:WidgetDemo.

 

二、准备素材,一个是Widget的图标,一个是Widget的背景。存放目录如下图:

 

 

三、修改string.xml文件如下:

  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>  

 

四、建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:

 

  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. />   

 

五、修改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/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>  

 

六、修改WidgetDemo.java代码如下:

  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.     
  15.     @Override  
  16.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  17.             int[] appWidgetIds) {  
  18.           
  19.         Timer timer = new Timer();  
  20.         timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 160000);  
  21.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  22.     }  
  23.       
  24.       
  25.     private class MyTime extends TimerTask{  
  26.         RemoteViews remoteViews;  
  27.         AppWidgetManager appWidgetManager;  
  28.         ComponentName thisWidget;  
  29.           
  30.         public MyTime(Context context,AppWidgetManager appWidgetManager){  
  31.             this.appWidgetManager = appWidgetManager;  
  32.             remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);  
  33.               
  34.             thisWidget = new ComponentName(context,WidetDemo.class);  
  35.         }  
  36.         public void run() {  
  37.               
  38.             Date date = new Date();  
  39.             Calendar calendar = new GregorianCalendar(2010,06,11);  
  40.             long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;  
  41.             remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" + days+"天");  
  42.             appWidgetManager.updateAppWidget(thisWidget, remoteViews);  
  43.               
  44.         }  
  45.           
  46.     }  
  47.     
  48. }  

 

七、修改配置文件AndroidManifest.xml,代码如下:

  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.         <receiver 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.         </receiver>  
  16.     </application>  
  17.     <uses-sdk android:minSdkVersion="7" />  
  18. </manifest>   

 

八、点击运行(Ctrl+F11),之,运行成功后,我们长时间点击桌面,会出现如下俩个,依次点击,就可以看到最上面的效果图:

 

 

今天就到这里了,我困了呵呵,我发现时间好像不对劲,lol~我也不去多想了,大家知道的告诉我下!对日历这些东西不是太了解,谢谢!!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值