主程序,运行窗口小部件的时候调用的类。需要在manifest.xml中注册。
package com.example.widgettext;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
public class AppClockWidgetProvider extends AppWidgetProvider {
private static RemoteViews mRemoteViews;
private static Timer mTimer;
// mAppWidgetArray 保存窗口部件实例的数组
private static ArrayList<int[]> mAppWidgetArray = new ArrayList<int[]>();
// 当最后一个窗口部件实例被移除时调用
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
stopTimerTask();
}
// 当第一个窗口部件实例被创建时调用
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
startTimerTask(context);
}
// 窗口部件实例创建时调用
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
updateAppWidget(context, appWidgetManager, appWidgetIds);
addInstance(appWidgetIds);
}
// 窗口部件实例删除时调用
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
removeInstance(appWidgetIds);
}
// 更新单个窗口部件实例
public static void updateAppWidget(Context context,
AppWidgetManager appWidgeManger, int[] appWidgetIds) {
mRemoteViews = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
MyTimeAdapter timeAdapter = new MyTimeAdapter();
String timeString = timeAdapter.formatTime();
mRemoteViews.setTextViewText(R.id.myTextView, timeString);
appWidgeManger.updateAppWidget(appWidgetIds, mRemoteViews);
}
// 更新所有窗口部件实例
public static void RefreshAllWidget(Context context) {
AppWidgetManager appWidgetManger = AppWidgetManager
.getInstance(context);
int instanceCount = mAppWidgetArray.size();
for (int i = 0; i < instanceCount; i++) {
int[] appWidgetIds = mAppWidgetArray.get(i);
updateAppWidget(context, appWidgetManger, appWidgetIds);
}
}
// 增加窗口部件实例
public static void addInstance(int[] appWidgetIds) {
if (mAppWidgetArray != null) {
mAppWidgetArray.add(appWidgetIds);
}
}
// 删除窗口部件实例
public static void removeInstance(int[] appWidgetIds) {
if (mAppWidgetArray != null) {
mAppWidgetArray.remove(appWidgetIds);
}
}
// 开启刷新窗口部件的定时器
public static void startTimerTask(final Context context) {
if (mTimer == null) {
mTimer = new Timer();
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
AppClockWidgetProvider.RefreshAllWidget(context);
}
};
mTimer.schedule(mTimerTask, 0, 1000);
}
}
// 关闭刷新窗口部件的定时器
public static void stopTimerTask() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
}
在主程序中用到的一个adapter
package com.example.widgettext;
import java.util.Calendar;
public class MyTimeAdapter {
private Calendar mCalendar;
public MyTimeAdapter() {
long time = System.currentTimeMillis();
mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(time);
}
public int getHour() {
return mCalendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinutes() {
return mCalendar.get(Calendar.MINUTE);
}
public int getSecond() {
return mCalendar.get(Calendar.SECOND);
}
public String formatTime() {
String timeString = getHour() + ":" + getMinutes() + ":" + getSecond();
return timeString;
}
}
在res/xml文件夹下配置的一个文件,用来确定窗口小部件的大小。
<?xml version="1.0" encoding="UTF-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/activity_main"
android:minHeight="100.0dip"
android:minWidth="100.0dip"
android:updatePeriodMillis="1000" />
在manifest.xml的内容(与运行activity的程序不同,这里的action配置为receiver)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgettext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.example.widgettext.AppClockWidgetProvider"
android:label="text" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/homewidget_provider" />
</receiver>
</application>
</manifest>
参考:http://www.eoeandroid.com/thread-70907-1-1.html