Android的桌面组件App Widget的使用方法 AppWidgetProvider RemoteViews

What is App Widget?



public class AppWidgetProviderInfo extends Object implements Parcelable

Describes the meta data for an installed AppWidget provider. The fields in this class correspond to the fields in the xml tag. AppWidgetProvider

public class AppWidgetProvider extends BroadcastReceiver

A convenience class to aid in implementing an AppWidget provider. Everything you can do with AppWidgetProvider, you can do with a regularBroadcastReceiver. AppWidgetProvider merely parses the relevant fields out of the Intent that is received in onReceive(Context,Intent), and calls hook methods with the received extras.

创建一个简单的App Widget的基本步骤:

① 在res文件夹下创建一个xml文件夹,里面创建一个xml文件(这里创建了一个test_appwidget_info.xml文件)用于提供元数据:

<appwidget-provider xmlns:android=”"

其中的test_appwidget为App Widget的布局文件,在第二步创建。 ② 创建App Widget的布局文件test_appwidget.xml:

<?xml version=”1.0” encoding=”utf-8”?>

③ 创建一个类继承AppWidgetProvider并覆盖里面的几个常用的方法:

onDeleted(Context context, int[] appWidgetIds) 删除App Widget是调用此方法 onDisabled(Context context) 最后一个App Widget实例删除后调用此方法 onEnabled(Context context) App WIdget实例第一次被创建是调用此方法 onReceive(Context context, Intent intent) 接收广播事件 onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 到达指定更新时间或用户向桌面添加了App Widget时调用此方法

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;

public class TestAppWidgetProvider extends AppWidgetProvider{

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int\[\] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
public void onDeleted(Context context, int\[\] appWidgetIds) {
    super.onDeleted(context, appWidgetIds);
public void onDisabled(Context context) {
public void onEnabled(Context context) {


④ 在AndroidManifest.xml文件中声明一个receiver,注意intent和meta-data子标签的设置:


public final class PendingIntent extends Object implements Parcelable

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time. RemoteViews

public class RemoteViews extends Object implements Parcelable LayoutInflater.Filter

A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.

使用PendingIntent和RemoteViews对App Widget绑定监听器:


getActivity(Context context, int requestCode, Intent intent, int flags) getBroadcast(Context context, int requestCode, Intent intent, int flags) getService(Context context, int requestCode, Intent intent, int flags)

在创建一个简单的App Widget的基本步骤基础之上进行一下操作: ① 在test_appwidget.xml文件中添加一个按钮

arthinking wechat