android 窗口小部件(例子)

主程序,运行窗口小部件的时候调用的类。需要在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值