Android Widget开发案例(伦敦奥运会倒计时!)

河北衡水武强 3月16日20:21

效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0064/9943/9556484d-4d8c-3290-9a47-13d4da8977bb.jpg[/img]
[/img]

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



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


[img]
[img]http://dl.iteye.com/upload/attachment/0064/9945/8cb7e33f-4ddc-3095-a884-2c6fce018d45.jpg[/img]
[/img]

三、修改string.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, WidetDemo!</string>
<string name="app_name">DaysToWorldCup</string>
</resources>



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



五、修改main.xml布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/wordcup"
>
<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"
/>
</LinearLayout>


六、修改WidgetDemo.java代码如下:
package com.android.tutor;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
public class WidetDemo extends AppWidgetProvider {
/** Called when the activity is first created. */

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {

Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context,appWidgetManager), 1, 60000);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}


private class MyTime extends TimerTask{
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;

public MyTime(Context context,AppWidgetManager appWidgetManager){
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);

thisWidget = new ComponentName(context,WidetDemo.class);
}
public void run() {

Date date = new Date();
Calendar calendar = new GregorianCalendar(2010,06,11);
long days = (((calendar.getTimeInMillis()-date.getTime())/1000))/86400;
remoteViews.setTextViewText(R.id.wordcup, "距离南非世界杯还有" + days+"天");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);

}

}

}



七、修改配置文件AndroidManifest.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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"
/>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>



八、点击运行(Ctrl+F11),之,运行成功后,我们长时间点击桌面,会出现如下俩个,依次点击,就可以看到最上面的效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0064/9947/5136ff70-df13-3454-948f-eeaa5edefbf7.jpg[/img]
[/img][img]
[img]http://dl.iteye.com/upload/attachment/0064/9949/7d5e039a-0b8f-3c96-9bb1-f9b0f968270e.jpg[/img]
[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值