--------------------------------------------AndroidManifest.xml------------------------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ch14"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.ch14" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name=".MyAppWidgetProvider" >
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_my" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
--------------------------------------------XML appwidget_my.xml--------------------------
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth = "294dp"
android:minHeight = "72dp"
android:updatePeriodMillis = "0"
android:initialLayout = "@layout/myappwidgetlayout"
>
</appwidget-provider>
<!-- updatePeriodMillis = 0 为手动更新 -->
--------------------------------------------Layout activity_main.xml------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
</RelativeLayout>
--------------------------------------------Layout myappwidgetlayout.xml-----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/txtapp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="send........" >
</Button>
</LinearLayout>
--------------------------------------------MainActivity.java---------------------------------
package com.ch14;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
--------------------------------------------MyAppWidgetProvider.java-----------------------
package com.ch14;
import com.ch14.util.LogOut;
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.widget.RemoteViews;
/**
*
* 项目名称:com.ch14
* 类名称:MyAppWidgetProvider
* 类描述: 自定义窗口小部件
* 创建人:方勇
* 创建时间:2012-11-22 下午9:05:00
* Copyright (c) 方勇-版权所有
*/
public class MyAppWidgetProvider extends AppWidgetProvider {
/* 接受广播事件 */
@Override
public void onReceive(Context context, Intent intent) {
LogOut.out(this, "onReceive()");
/* 只能通过远程对象来设置MyAppWidgetProvider中的控件状态 */
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.myappwidgetlayout);
/* 管理AppWidgetProvider,以便进行更新操作 */
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
/* 获得本程序创建的appwidget */
ComponentName componentName = new ComponentName(context, MyAppWidgetProvider.class);
/* 更新appwidget */
appWidgetManager.updateAppWidget(componentName, remoteViews);
super.onReceive(context, intent);
}
/* 组件被放置到桌面上时,或者刷新时间到达时调用 */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
LogOut.out(this, "onUpdate()");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.myappwidgetlayout);
Intent intent = new Intent();
intent.setAction("com.ch14");
/*
* 延迟执行的INTENT
*
* 1、常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有更新的时候需要用到这个flag去更新你的描述,
* 否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。
*
* 2、使用FLAG_CANCEL_CURRENT也能做到更新extras,只不过是先把前面的extras清除
*
* 3、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否new一个Intent,
* FLAG_UPDATE_CURRENT能够new一个Intent,而
* FLAG_CANCEL_CURRENT则不能,只能使用第一次的Intent。
*
* 4、(比较少用)
* FLAG_ONE_SHOT获取的PendingIntent只能使用一次,再使用PendingIntent也将失败
* FLAG_NO_CREAT获取的PendingIntent描述的Intent不存在则返回NULL值.
*
*/
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
/* 通过PendingIntent和mRemoteViews.setOnClickPendingIntent为AppWidget中的Button设置监听器 */
remoteViews.setOnClickPendingIntent(R.id.txtapp, pendingIntent);
remoteViews.setTextViewText(R.id.txtapp, "A07");
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/* 删除一个AppWidget组件时被调用 */
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
LogOut.out(this, "onDeleted()");
super.onDeleted(context, appWidgetIds);
}
/* AppWidget的实例第一次被创建时调用 */
@Override
public void onEnabled(Context context) {
LogOut.out(this, "onEnabled()");
super.onEnabled(context);
}
/* 最后一个AppWidget组件被删除时调用 */
@Override
public void onDisabled(Context context) {
LogOut.out(this, "onDisabled()");
super.onDisabled(context);
}
}
--------------------------------------------LogOut.java---------------------------------------
package com.ch14.util;
import android.util.Log;
/**
*
* 项目名称:com.ch14
* 类名称:LogOut
* 类描述: 指定日志输出格式
* 创建人:方勇
* 创建时间:2012-11-22 下午9:35:41
* Copyright (c) 方勇-版权所有
*/
public class LogOut {
public static final String TAG = "A07";
private static final boolean DEBUG = true;
public static void out(Object obj, String info) {
if (DEBUG) {
String tag = TAG;
if (info.startsWith("length")) {
tag = TAG + "_TMP";
}
if (obj instanceof String) {
Log.d(tag, ((String) obj) + "->" + info);
} else {
Log.d(tag, obj.getClass().toString().substring(6) + "->" + info);
}
}
}
public static void out(Object obj, String tag, String info) {
if (DEBUG) {
if (obj instanceof String) {
Log.d(TAG + "_" + tag, ((String) obj) + "->" + info);
} else {
Log.d(TAG + "_" + tag, obj.getClass().toString().substring(6)
+ "->" + info);
}
}
}
}
--------------------------------------------结果------------------------------------------------
1、长按桌面(或选中桌面的某个位置,长按鼠标左键)
2、弹出对话框
3、选中后,桌面多了个按钮部件
4、双击AppWidget