本文主要是介绍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 stubsuper.onDisabled(context);stopTimerTask();}// 当第一个窗口部件实例被创建时调用public void onEnabled(Context context) {// TODO Auto-generated method stubsuper.onEnabled(context);startTimerTask(context);}// 窗口部件实例创建时调用public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {// TODO Auto-generated method stubsuper.onUpdate(context, appWidgetManager, appWidgetIds);updateAppWidget(context, appWidgetManager, appWidgetIds);addInstance(appWidgetIds);}// 窗口部件实例删除时调用public void onDeleted(Context context, int[] appWidgetIds) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void run() {// TODO Auto-generated method stubAppClockWidgetProvider.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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><receiverandroid:name="com.example.widgettext.AppClockWidgetProvider"android:label="text" ><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/homewidget_provider" /></receiver></application></manifest>
参考:http://www.eoeandroid.com/thread-70907-1-1.html
这篇关于android 窗口小部件(例子)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!