Android应用小工具(窗口小部件)
Widget是可以在其他應(yīng)用程序被嵌入和接收定期更新的微型應(yīng)用程序視圖。
在創(chuàng)建一個(gè)應(yīng)用程序窗口小部件,需要滿(mǎn)足以下條件:
AppWidgetProviderInfo--描述元數(shù)據(jù)為應(yīng)用窗口小部件,如布局,更新頻率和AppWidgetProvider類(lèi),應(yīng)在XML定義。
AppWidgetProvider類(lèi)實(shí)現(xiàn)--定義的基本方法,編程與應(yīng)用程序widget界面,基于廣播事件,當(dāng)應(yīng)用程序的Widget被更新,啟用,禁用和刪除將收到廣播。
查看布局,定義了應(yīng)用程序插件的初始布局,在XML中定義
接下來(lái)是設(shè)置一個(gè)小部件的過(guò)程
首先在清單中聲明一個(gè)應(yīng)用程序的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>元件必須包括一個(gè)<action>與元件android:name屬性,指定AppWidgetProvider接受ACTION_APPWIDGET_UPDATE廣播,必須顯式聲明只播出。<meta-data>元素指定的AppWidgetProviderInfo資源需要以下屬性:
android:name指定元數(shù)據(jù)名稱(chēng),使用androi.appwidget.provider標(biāo)識(shí)數(shù)據(jù)的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> 創(chuàng)建 應(yīng)用程序窗口小部件布局必須在XML中定義一個(gè)初始布局為應(yīng)用程序窗口小部件,保存至res/layout/目錄
窗口小部件支持以下布局類(lèi):
FrameLayout
LinearLayout
RelativeLayout
GridLayout
添加邊距應(yīng)用小工具
1.設(shè)置應(yīng)用程序的targetSdkkVersion14或更大
2.創(chuàng)建一個(gè)布局,如下:
<FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:padding="@dimen/widget_margin"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:background="@drawable/my_widget_background">…</LinearLayout></FrameLayout>3.創(chuàng)建兩個(gè)維度的資源,一個(gè)在res/values/, 一個(gè)在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類(lèi)當(dāng)應(yīng)用程序的Widget被更新,刪除,啟用以及禁用時(shí),收到的AppWidgetProvider將調(diào)用以下方法:
onUpdate()---定義更新應(yīng)用程序的間隔updatePeriodMills中的AppWidgetProviderInfo屬性
onAppWidgetOptionsChanged()---通過(guò)調(diào)用getAppWidgetOptions()以獲得尺寸范圍,將會(huì)返回一個(gè)Bundle包含OPTION_APPWIDGET_MIN_WIDTH, OPTION_APPWIDGET_MIN_HEIGHT, OPTION_APPWIDGET_MAC_WIDTH, OPTION_APPWIDGET_MAC_HEIGHT
onDeleted(Context, int[])---每一個(gè)App的Widget從應(yīng)用的Widget主機(jī)中刪除的時(shí)間
onEnabled(Context)---當(dāng)一個(gè)實(shí)例的應(yīng)用程序的Widget是首次創(chuàng)建需要調(diào)用的
onDisabled(Context)---當(dāng)應(yīng)用的Widget的最后一個(gè)實(shí)例從應(yīng)用的Widget的主機(jī)中刪除。
onReceivce(context, Intent)---每個(gè)廣播和上述的每個(gè)回調(diào)方法之前調(diào)用。通常默認(rèn)實(shí)現(xiàn),不需要調(diào)用。
最重要的回調(diào)是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 providerfor (int i=0; i<N; i++) {int appWidgetId = appWidgetIds[i];// Create an Intent to launch ExampleActivityIntent 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 buttonRemoteViews 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 widgetappWidgetManager.updateAppWidget(appWidgetId, views);}} } 接收應(yīng)用的Widget廣播IntentAppWidgetProvider是一個(gè)簡(jiǎn)易的類(lèi),可以重寫(xiě)B(tài)roadcastReceiver或重寫(xiě)onReceive(Context, Intent)回調(diào),需要關(guān)心的Intent有:
ACTION_APPWIDGET_UPDATE
ACTION_APPWIDGET_DELETE
ACTION_APPWIDGET_ENABLED
ACTION_APPWIDGET_DISABLES
ACTION_APPWIDGET_OPTIONS_CHANGED
創(chuàng)建一個(gè)應(yīng)用程序窗口小部件配置Activity
配置活動(dòng)被宣布為Android清單文件中的正?;顒?dòng),當(dāng)被應(yīng)用的Widget主機(jī)與發(fā)起ACTION_APPWIDGET_CONFIGURE行為,Activity需要接收這個(gè)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>從配置活動(dòng)更新應(yīng)用程序的Widget
更新應(yīng)用程序的Widget和關(guān)閉配置活動(dòng)的程序的總結(jié)
1.首先,啟動(dòng)活動(dòng)的Intent應(yīng)用的WidgetID:
Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) {mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID); }2.執(zhí)行應(yīng)用程序窗口小部件配置
3.當(dāng)配置完成后,通過(guò)調(diào)用得到AppWidgetManager的一個(gè)實(shí)例getInstance(Context)
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);4.更新應(yīng)用的Widget使用RemoteViews通過(guò)調(diào)用布局updateAppWidget(int, RemoteViews)
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget); appWidgetManager.updateAppWidget(mAppWidgetId, views);5.最后創(chuàng)建返回Intent,隨著活動(dòng)的結(jié)果集,完成活動(dòng)Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); setResult(RESULT_OK, resultValue); finish();
在XML中設(shè)置:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"...android:previewImage="@drawable/preview"> </appwidget-provider> 使用App小工具小工具使用RemoteViewsService,以顯示遠(yuǎn)程數(shù)據(jù),將提供的數(shù)據(jù)顯示在以下視圖類(lèi)型中:
ListView/ GridView/ StackView/ AdapterViewFlipper
RemoteViewsService是允許一個(gè)遠(yuǎn)程適配器請(qǐng)求服務(wù)RemoteViews的對(duì)象,RemoteViewsFactory是集合視圖之間的適配器的接口。
public class StackWidgetService extends RemoteViewsService {@Overridepublic 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.}
總結(jié)
以上是生活随笔為你收集整理的Android应用小工具(窗口小部件)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android内容提供程序
- 下一篇: JDBC详解及总结