Android应用小工具(窗口小部件)
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.創建一個布局,如下:
<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.創建兩個維度的資源,一個在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 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);}} } 接收應用的Widget廣播IntentAppWidgetProvider是一個簡易的類,可以重寫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 {@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.}
總結
以上是生活随笔為你收集整理的Android应用小工具(窗口小部件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android内容提供程序
- 下一篇: JDBC详解及总结