Widget是可以在其他应用程序被嵌入和接收定期更新的微型应用程序视图。
在创建一个应用程序窗口小部件,需要满足以下条件:
AppWidgetProviderInfo--描述元数据为应用窗口小部件,如布局,更新频率和AppWidgetProvider类,应在XML定义。
AppWidgetProvider类实现--定义的基本方法,编程与应用程序widget界面,基于广播事件,当应用程序的Widget被更新,启用,禁用和删除将收到广播。
查看布局,定义了应用程序插件的初始布局,在XML中定义
接下来是设置一个小部件的过程
首先在清单中声明一个应用程序的Widget:
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
<intent-filter>元件必须包括一个<action>与元件android:name属性,指定AppWidgetProvider接受ACTION_APPWIDGET_UPDATE广播,必须显式声明只播出。
<meta-data>元素指定的AppWidgetProviderInfo资源需要以下属性:
android:name指定元数据名称,使用androi.appwidget.provider标识数据的AppWidgetProviderInfo描述符。
android:resource指定AppWidgetProviderInfo资源位置。
添加AppWidgetProviderInfo:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/preview"
android:initialLayout="@layout/example_appwidget"
android:configure="com.example.android.ExampleAppWidgetConfigure"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
创建 应用程序窗口小部件布局
必须在XML中定义一个初始布局为应用程序窗口小部件,保存至res/layout/目录
窗口小部件支持以下布局类:
FrameLayout
LinearLayout
RelativeLayout
GridLayout
添加边距应用小工具
1.设置应用程序的targetSdkkVersion14或更大
2.创建一个布局,如下:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/my_widget_background">
…
</LinearLayout>
</FrameLayout>
3.创建两个维度的资源,一个在res/values/, 一个在res/valus-v14/
res/values/dimens.xml:
<dimen name="widget_margin">8dp</dimen>
res/values-v14/dimens.xml:
<dimen name="widget_margin">0dp</dimen>
使用的AppWidgetProvider类
当应用程序的Widget被更新,删除,启用以及禁用时,收到的AppWidgetProvider将调用以下方法:
onUpdate()---定义更新应用程序的间隔updatePeriodMills中的AppWidgetProviderInfo属性
onAppWidgetOptionsChanged()---通过调用getAppWidgetOptions()以获得尺寸范围,将会返回一个Bundle包含OPTION_APPWIDGET_MIN_WIDTH, OPTION_APPWIDGET_MIN_HEIGHT, OPTION_APPWIDGET_MAC_WIDTH, OPTION_APPWIDGET_MAC_HEIGHT
onDeleted(Context, int[])---每一个App的Widget从应用的Widget主机中删除的时间
onEnabled(Context)---当一个实例的应用程序的Widget是首次创建需要调用的
onDisabled(Context)---当应用的Widget的最后一个实例从应用的Widget的主机中删除。
onReceivce(context, Intent)---每个广播和上述的每个回调方法之前调用。通常默认实现,不需要调用。
最重要的回调是onUpdate()
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
接收应用的Widget广播Intent
AppWidgetProvider是一个简易的类,可以重写BroadcastReceiver或重写onReceive(Context, Intent)回调,需要关心的Intent有:
ACTION_APPWIDGET_UPDATE
ACTION_APPWIDGET_DELETE
ACTION_APPWIDGET_ENABLED
ACTION_APPWIDGET_DISABLES
ACTION_APPWIDGET_OPTIONS_CHANGED
创建一个应用程序窗口小部件配置Activity
配置活动被宣布为Android清单文件中的正常活动,当被应用的Widget主机与发起ACTION_APPWIDGET_CONFIGURE行为,Activity需要接收这个Intent:
<activity android:name=".ExampleAppWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
Activity必须在AppWidgetProviderInfo的XML文件中声明
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
...
android:configure="com.example.android.ExampleAppWidgetConfigure"
... >
</appwidget-provider>
从配置活动更新应用程序的Widget
更新应用程序的Widget和关闭配置活动的程序的总结
1.首先,启动活动的Intent应用的WidgetID:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
2.执行应用程序窗口小部件配置
3.当配置完成后,通过调用得到AppWidgetManager的一个实例getInstance(Context)
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
4.更新应用的Widget使用RemoteViews通过调用布局updateAppWidget(int, RemoteViews)
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
5.最后创建返回Intent,随着活动的结果集,完成活动
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
在XML中设置:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
...
android:previewImage="@drawable/preview">
</appwidget-provider>
使用App小工具
小工具使用RemoteViewsService,以显示远程数据,将提供的数据显示在以下视图类型中:
ListView/ GridView/ StackView/ AdapterViewFlipper
RemoteViewsService是允许一个远程适配器请求服务RemoteViews的对象,RemoteViewsFactory是集合视图之间的适配器的接口。
public class StackWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
//... include adapter-like methods here. See the StackView Widget sample.
}