想写个appWidget的玩玩,下面简单的写个demo,在Settings apk的工程中添加demo。步骤如下。
1 写一个类,继承于AppWidgetProvider
package com.android.settings.widget; 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.content.res.Resources; import android.os.Environment; import android.view.View; import android.widget.RemoteViews; import com.android.settings.R; public class DisplaySettingsAppWidgetProvider extends AppWidgetProvider { static final String TAG = "DisplaySettingsWidgetProvider"; public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { defaultAppWidget(context, appWidgetIds); } private void defaultAppWidget(Context context, int[] appWidgetIds) { final Resources res = context.getResources(); /*获取widget的布局,那么我们将可以对其做任何下做的操作了,包括监听、改变状态等 */ final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.display_widget); pushUpdate(context, appWidgetIds, views); } //更新显示啦 private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) { // Update specific list of appWidgetIds if given, otherwise default to all final AppWidgetManager gm = AppWidgetManager.getInstance(context); if (appWidgetIds != null) { gm.updateAppWidget(appWidgetIds, views); } else { gm.updateAppWidget(new ComponentName(context, this.getClass()), views); } } }
AppWidgetProvider继承于BroadcastReceiver,用于写appWidget的基类,主要提供的方法如下:
void | onDeleted( Context context, int[] appWidgetIds)
当AppWidget在桌面创建的图标被删除的时候,
响应
ACTION_APPWIDGET_DELETED
的broadcast广播消息。
|
void | onDisabled( Context context)
当AppWidget在桌面创建的最后一个图标被删除的时候,
响应
ACTION_APPWIDGET_DISABLED
的broadcast广播消息。
|
void | onEnabled( Context context)
当一个
AppWidget在桌面创建图标的时候,
响应 的broadcast广播消息。
|
void | onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
Called in response to the
ACTION_APPWIDGET_UPDATE broadcast
when this AppWidget provider is being asked to provide
RemoteViews for a set of AppWidgets.
Override this method to implement your own AppWidget functionality.
|
<receiver android:name=".widget.DisplaySettingsAppWidgetProvider"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/display_appwidget_info" /> </receiver>
在前面的介绍中,我们知道,AppWidgetProvider是继承于BroadcastReceiver的,那么必须在AndroidManifest文件中对其进行声明
而对于action的设置,可以根据自己的需求来设置了,这里的demo我只设置了 ACTION_APPWIDGET_UPDATE
,只接收update广播消息。
对于<meta-data>元素,主要是设置AppWidgetProviderInfo
:
android:name
- Specifies the metadata name. Use android.appwidget.provider
to identify the data as the AppWidgetProviderInfo
descriptor.android:resource
- 用于描述 AppWidgetProviderInfo
的本地xml文件,请看第三步,对该xml文件的介绍<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="@dimen/appwidget_min_width" android:minHeight="@dimen/appwidget_min_height" android:previewImage="@drawable/display_preview" android:initialLayout="@layout/display_widget" > </appwidget-provider>
我们主要看两个属性:
android:previewImage:从名字也将可以看出,这个是预览的图标,在桌面的“窗口小部件”中看到的预览图
android:initialLayout: 指定widget的布局文件啦
widget的显示布局文件,这里只简单定义了Button组件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/appwidget_bg_holo" > <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="app widget" /> </LinearLayout >