AppWidget 框架类
1.AppWidgetProvider
继承自 BroadcastRecevier , 在AppWidget 应用 update、enable、disable 和 delete 时接收通知。其中,onUpdate、onReceive 是最常用到的方法,它们接收更新通知。
继承自 AppWidgetProvider 可实现的方法为如下:
onDeleted(Context context, int[] appWidgetIds)
onDisabled(Context context)
onEnabled(Context context)
onReceive(Context context, Intent intent)
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
2.AppWidgetProvderInfo
描述 AppWidget 的大小、更新频率和初始界面等信息,以XML 文件形式存在于应用的 res/xml/目录下。
3.AppWidgetManger
负责管理 AppWidget ,向 AppwidgetProvider 发送通知。
bindAppWidgetId(int appWidgetId, ComponentName provider):通过给定的ComponentName 绑定appWidgetId
getAppWidgetIds(ComponentName provider):通过给定的ComponentName 获取AppWidgetId
getAppWidgetInfo(int appWidgetId):通过AppWidgetId 获取 AppWidget 信息
getInstalledProviders():返回一个List<AppWidgetProviderInfo>的信息
getInstance(Context context):获取 AppWidgetManger 实例使用的上下文对象
updateAppWidget(int[] appWidgetIds, RemoteViews views):通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
updateAppWidget(ComponentName provider, RemoteViews views):通过 ComponentName 对传进来的 RemoeteView 进行修改,并重新刷新AppWidget 组件
updateAppWidget(int appWidgetId, RemoteViews views):通过appWidgetId 对传进来的 RemoteView 进行修改,并重新刷新AppWidget 组件
4.RemoteViews
一个可以在其他应用进程中运行的类,向 AppWidgetProvider 发送通知。
Demo
一个简单的AppWidget步骤
1、在res下新建xml文件夹,然后新建appwidget_provider.xml
<?xml version="1.0" encoding="UTF-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="60dp" android:minHeight="30dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/widget_main"> </appwidget-provider>
2、在layout文件夹下新建布局文件widget_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:text="点点点我" > </TextView> </LinearLayout>
3、定义类继承自AppWidgetProvider
public class MyAppWidgetProvider extends AppWidgetProvider { private RemoteViews remoteViews; @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (remoteViews == null) { remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main); } if (intent.getAction().equals("com.example.widgettest")) { if (CommonValiable.isChange) { remoteViews.setTextViewText(R.id.tv, "点点点我"); } else { remoteViews.setTextViewText(R.id.tv, "买买买我"); } Toast.makeText(context, Boolean.toString(CommonValiable.isChange), Toast.LENGTH_LONG).show(); CommonValiable.isChange = !CommonValiable.isChange; } AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context); int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, MyAppWidgetProvider.class)); appWidgetManger.updateAppWidget(appIds, remoteViews); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; updateAppWidget(context, appWidgetManager, appWidgetId); } } public void updateAppWidget(Context context, AppWidgetManager appWidgeManger, int appWidgetId) { remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main); Intent intent = new Intent("com.example.widgettest"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.tv, pendingIntent); appWidgeManger.updateAppWidget(appWidgetId, remoteViews); } }
4、在清单文件的application节点下声明如下
<receiver android:name=".MyAppWidgetProvider" > <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider" > </meta-data> <intent-filter> <action android:name="com.example.widgettest" /> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver>