Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 窗口小部件(例子)

android 窗口小部件(例子)

編輯:關於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 mAppWidgetArray = new ArrayList();

	// 當最後一個窗口部件實例被移除時調用
	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文件夾下配置的一個文件,用來確定窗口小部件的大小。

 

 



在manifest.xml的內容(與運行activity的程序不同,這裡的action配置為receiver)

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved