Android Widget开发

把AppWidget添加到桌面后,点击AppWidget后AppWidget文本会轮回改变

main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:id="@+id/layout" android:layout_height="fill_parent">
	<Button android:text="@string/login" android:id="@+id/TextView01"
		android:clickable="true" android:layout_width="wrap_content"
		android:layout_height="wrap_content"></Button>
</LinearLayout>


res/xml/widget.xml布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- AppWidgetProvderInfo: 描述AppWidget的大小、更新频率和初始界面等信息,以XML文件形式存在于应用的res/xml/目录下。 
	注意:SDK1.5之后此android:updatePeriodMillis就失效了,要自己创建service更新 -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
	android:minWidth="60dp" android:minHeight="30dp"
	android:updatePeriodMillis="86400000" android:initialLayout="@layout/main">
</appwidget-provider>


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="com.iaiai.activity" android:versionCode="1"
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="8" />
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<!-- <activity android:name="IaiaiActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
			android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" 
			/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 
			</activity> -->

		<receiver android:name=".IaiaiWidget" android:label="添加桌面控件">
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/widget"></meta-data>
			<intent-filter>
				<action android:name="com.iaiai.widget.click"></action>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
		</receiver>
	</application>
</manifest>  


变量类UtilTool.java:用来控件文本改变:
package com.iaiai.activity;

/**
 * 
 * <p>
 * Title: UtilTool.java
 * </p>
 * <p>
 * E-Mail: [email protected]
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-6-24
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class UtilTool {

	public static boolean isChange = true;

}


IaiaiWidget.java类:
package com.iaiai.activity;

import android.app.PendingIntent;
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.widget.RemoteViews;

/**
 * 
 * <p>
 * Title: IaiaiWidget.java
 * </p>
 * <p>
 * E-Mail: [email protected]
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-6-24
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class IaiaiWidget extends AppWidgetProvider {

	private static final String CLICK_NAME_ACTION = "com.iaiai.widget.click";

	private static RemoteViews rv;

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);

		Log.i("**************", "onReceive");

		if (rv == null) {
			rv = new RemoteViews(context.getPackageName(), R.layout.main);
		}
		if (intent.getAction().equals(CLICK_NAME_ACTION)) {
			if (UtilTool.isChange) {
				rv.setTextViewText(R.id.TextView01, "abc");
			} else {
				rv.setTextViewText(R.id.TextView01, "123");
			}
			UtilTool.isChange = !UtilTool.isChange;
			AppWidgetManager appWidgetManger = AppWidgetManager
					.getInstance(context);
			int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
					context, IaiaiWidget.class));
			appWidgetManger.updateAppWidget(appIds, rv);
		} else {
			super.onReceive(context, intent);
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("**************", "onUpdate");
		final int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateAppWidget(context, appWidgetManager, appWidgetId);
		}
	}

	private void updateAppWidget(Context context,
			AppWidgetManager appWidgeManger, int appWidgetId) {
		rv = new RemoteViews(context.getPackageName(), R.layout.main);
		Intent intentClick = new Intent();
		intentClick.setAction(CLICK_NAME_ACTION);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intentClick, 0);
		rv.setOnClickPendingIntent(R.id.TextView01, pendingIntent);
		appWidgeManger.updateAppWidget(appWidgetId, rv);
	}

}


运行结果:
Android Widget开发
Android Widget开发

你可能感兴趣的:(java,android,xml,qq,ITeye)