当前位置:首页 >> IT/计算机 >>

Android高手进阶教程(八)之----Android Widget开发案例(世界杯倒计时!)


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

下面是 Demo 的详细步骤: 一、新建一个 Android 工程命名为:WidgetDemo. 二、准备素材,一个是 Widget 的图标,一个是 Widget 的背景。存放目录如下图:

三、修改 string.xml 文件如下:
1 2 3 4 5 6 7 8 9 10 view plaincopy to clipboardprint? <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, WidetDemo!</string> <string name="app_name">DaysToWorldCup</string> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, WidetDemo!</string> <string name="app_name">DaysToWorldCup</string>

11 </resources>

四、建立 Widget 内容提供者文件,我们在 res 下建立 xml 文件夹,并且新建一个 widget_provider.xml 代码入下:
12 view plaincopy to clipboardprint? 13 <?xml version="1.0" encoding="utf-8"?> 14 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 15 16 17 18 19 /> 20 <?xml version="1.0" encoding="utf-8"?> android:minWidth="50dip" android:minHeight="50dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main"

21 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 22 23 24 25 android:minWidth="50dip" android:minHeight="50dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main"

26 />

五、修改 main.xml 布局,代码如下:
27 view plaincopy to clipboardprint? 28 <?xml version="1.0" encoding="utf-8"?> 29 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 30 31 32 33 34 36 37 38 39 40 41 42 android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/wordcup" >

35 <TextView android:id="@+id/wordcup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12px" android:textColor="#ff0000" />

43 </LinearLayout> 44 <?xml version="1.0" encoding="utf-8"?> 45 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 46 47 48 49 50 52 53 54 55 56 57 58 60 android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/wordcup" >

51 <TextView android:id="@+id/wordcup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12px" android:textColor="#ff0000" />

59 </LinearLayout>

61 六、修改 WidgetDemo.java 代码如下: 62 view plaincopy to clipboardprint? 63 package com.android.tutor; 64 import java.util.Calendar; 65 import java.util.Date; 66 import java.util.GregorianCalendar; 67 import java.util.Timer; 68 import java.util.TimerTask; 69 import android.appwidget.AppWidgetManager; 70 import android.appwidget.AppWidgetProvider; 71 import android.content.ComponentName; 72 import android.content.Context; 73 import android.widget.RemoteViews; 74 public class WidetDemo extends AppWidgetProvider { 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 Date date = new Date(); Calendar calendar = new GregorianCalendar(2010,06,11); } public void run() { thisWidget = new ComponentName(context,WidetDemo.class); public MyTime(Context context,AppWidgetManager appWidgetManager){ this.appWidgetManager = appWidgetManager; remoteViews = new private class MyTime extends TimerTask{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; } Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), @Override public void onUpdate(Context context, AppWidgetManager /** Called when the activity is first created. */

appWidgetManager, int[] appWidgetIds) {

1, 60000); super.onUpdate(context, appWidgetManager, appWidgetIds);

RemoteViews(context.getPackageName(),R.layout.main);

102 103 days+"天"); 104 105 106 107 108 109 110 } } }

long days =

(((calendar.getTimeInMillis()-date.getTime())/1000))/86400; remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" +

appWidgetManager.updateAppWidget(thisWidget, remoteViews);

111 package com.android.tutor; 112 import java.util.Calendar; 113 import java.util.Date; 114 import java.util.GregorianCalendar; 115 import java.util.Timer; 116 import java.util.TimerTask; 117 import android.appwidget.AppWidgetManager; 118 import android.appwidget.AppWidgetProvider; 119 import android.content.ComponentName; 120 import android.content.Context; 121 import android.widget.RemoteViews; 122 public class WidetDemo extends AppWidgetProvider { 123 124 125 @Override 126 public void onUpdate(Context context, AppWidgetManager appWidgetManager, 127 128 129 130 131 133 134 135 private class MyTime extends TimerTask{ 136 137 138 139 140 141 142 public MyTime(Context context,AppWidgetManager appWidgetManager){ this.appWidgetManager = appWidgetManager; remoteViews = new RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; Timer timer = new Timer(); timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, int[] appWidgetIds) { /** Called when the activity is first created. */

60000); super.onUpdate(context, appWidgetManager, appWidgetIds); 132 }

RemoteViews(context.getPackageName(),R.layout.main); 143 144 145 146 147 148 149 150 151 天"); 152 153 154 155 156 } 157 158 } } appWidgetManager.updateAppWidget(thisWidget, remoteViews); Date date = new Date(); Calendar calendar = new GregorianCalendar(2010,06,11); long days = thisWidget = new ComponentName(context,WidetDemo.class); } public void run() {

(((calendar.getTimeInMillis()-date.getTime())/1000))/86400; remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" + days+"

七、修改配置文件 AndroidManifest.xml,代码如下:
159 view plaincopy to clipboardprint? 160 <?xml version="1.0" encoding="utf-8"?> 161 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 /> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> package="com.android.tutor" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon"

android:label="@string/app_name"> <receiver android:name=".WidetDemo" android:label="@string/app_name"> <intent-filter> <action

android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider"

177 </manifest> 178 <?xml version="1.0" encoding="utf-8"?> 179 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 180 package="com.android.tutor"

181 182 183 184 185 186 187 188 189 190 191 192 193 194

android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon"

android:label="@string/app_name"> <receiver android:name=".WidetDemo" android:label="@string/app_name"> <intent-filter> <action

android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /> </receiver> </application> <uses-sdk android:minSdkVersion="7" />

195 </manifest>

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

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



相关文章:
八Android Widget开发案例(世界杯倒计时!)
Android高手进阶教程(八)之... 3页 5财富值 Android Widget开发指导 5页 2财富...八Android Widget开发案例(世界杯倒计时!)隐藏>> 今天我们要写一下 Android ...
Android倒计时效果
Android 倒计时效果在 Android 开发中,我们一般用 3 个简单的方法来实现定时器...Android高手进阶教程(八... 8页 免费 Android实现计时与倒计时... 1页 免费...
Android实现计时与倒计时限时抢购的几种方法
Android手机安装软件的几... 1页 免费 八Android Widget开发案... 6页 免费...android倒计时功能的实现... 2页 1下载券 Android高手进阶教程(八... 8页 ...
android开发相关网站链接
android/#intro 8、安卓开发视频教程和书籍下载 http...诸多开发案例、代码源,程序员之间的吧帖,以及安卓...适 合初学者和高手进阶的好地方! http://www.apk...
更多相关标签: