APP Widget的开发

Android中经常用到APP Widget,如时钟,天气预报等。

长按屏幕,在弹出的对话框中选择“窗口小部件”,然后就列出了可选择的小部件,这些小部件就是APP Widget。

本文开发一个APP Widget,在屏幕上显示当前的时间,并且每秒更新一次。

开发APP Widget需要以下三个xml文件。

(1)AndroidManifest.xml,这个是所有APP都有的文件,APP Widget的AndroidManifest.xml和其他的AndroidManifest.xml有所不同。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hzhi.time_widget"
    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="MyTime" android:label="MyTime">
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
			<meta-data android:name="android.appwidget.provider" android:resource="@xml/my_time" />
		</receiver> 
		
		<service android:name="MyTime$MyService" />

    </application>

</manifest>

其中,一个Receiver就代表一个APP Widget,如果想在一个工程里面开发多个APP Widget,多写几个Receiver就可以。<receiver android:name="MyTime" android:label="MyTime">表明该APP Widget的名称和标签。

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />表明接收APPWIDGET_UPDATE,这是必须有的,否则APP Widget无法更新。

<meta-data android:name="android.appwidget.provider" android:resource="@xml/my_time" />指明了APP Widget的信息文件为@xml/my_time,这也是下面将介绍的。

(2)APP Widget的信息文件,该文件是APP Widget所特有的。在res文件夹下面新建一个xml文件夹,在里面新建一个my_time.xml文件,即是APP Widget的信息文件。

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:minWidth="220dip"
	android:minHeight="146dip"
	android:updatePeriodMillis="0"
	android:initialLayout="@layout/my_time" 
/>

该文件设置了APP Widget的长和宽(长和宽的值应为74*n-2),以及更新间隔时间(android:updatePeriodMillis,本例使用Timer更新,所以设置为0),最后的android:initialLayout="@layout/my_time" 指明了APP Widget的布局文件,也就是显示在桌面上的布局。

(3)APP Widget的布局文件,本例为my_time.xml,基本和其他APP的布局文件一样,区别就在于整个布局文件只有一个TextView。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:text="Starting..."
    android:textColor="#FFFF00"
    android:textSize="20pt" />

最后是java文件MyTime.java: 

package com.hzhi.time_widget;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MyTime extends AppWidgetProvider {
	
	Timer timer;
	Context context;

	//onUpdate
	@Override
	public void onUpdate(Context con, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		context = con;
		Intent intent = new Intent(context, MyService.class);
		timer = new Timer();
		timer.schedule(timertask, 0, 1000);
	}
	//MyService服务程序
	public static class MyService extends Service {
		@Override
		public void onStart(Intent intent, int startId) {
			RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_time);
			remoteViews.setTextViewText(R.id.TextView01, new Date().toLocaleString());			
			ComponentName thisWidget = new ComponentName(this, MyTime.class);
			AppWidgetManager manager = AppWidgetManager.getInstance(this);
			manager.updateAppWidget(thisWidget, remoteViews);
		}
		@Override
		public IBinder onBind(Intent intent) {
			return null;
		}
	};
	
	// Handler
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			Intent intent = new Intent(context, MyService.class);
			context.startService(intent);
		}
	};
	
	private TimerTask timertask = new TimerTask(){
		public void run(){
			Message message = new Message();
			handler.sendMessage(message);
		}
	};
	
}

该Java文件使用Timer,每秒钟开始一次服务MyService。

MyService获得当前的时间,并且以AppWidgetManager.updateAppWidget()方法更新APP Widget所显示的时间。

运行APP,并添加到桌面上。

原文地址:https://www.cnblogs.com/mstk/p/3534326.html