先來看下. 這是仿三星FM收音機的一個widget.
1. 首先在widget建立一個布局檔案: layout/widget.xml.
2. 在xml中建立xml/radioinfo.xml:
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="50.0dip" android:minWidth="300.0dip" android:previewImage="@drawable/fm_radio_widget_preview" android:resizeMode="vertical" />
屬性解釋: android:initialLayout="@layout/widget" : 指定widget的布局檔案
android:previewImage="@drawable/fm_radio_widget_preview" 指定widget在手機小組件中的縮圖.
3. 建立一個繼承AppWidgetProvider類FMRadioWidgetProvider.java, 並實現onUpdate , onReceive ,這兩個函數.
3.1 AppWidgetProvider解釋: 這個類其實是一個廣播接收器, 當widget被拖放到案頭的時候會首先調用onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)函數. 所以 , 一般在這個函數中做初始化操作.
3.2 為按鈕添加事件:
Intent intent = new Intent(FMRadioService.ACTION_FM_NEXT); intent.setClass(context, FMRadioService.class); pIntent = PendingIntent.getService(context, 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.ic_arrow_right, pIntent);
當點擊這個這個按鈕的時候, 會發送一個Intent並FMRadioService類.
這種方式綁定的事件只能適用於單個View. 對於像ListView, GridView這樣的集合的item要綁定事件的話不適用. 這點後面講到.
3.3 為組件設定相關屬性, 例如給TextView設定文本:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);remoteViews.setTextViewText(R.id.txt_frequency, String.valueOf(freq));
最後: 要記得updateWidget一下: AppWidgetManager.getInstance(context).updateAppWidget(appWidgetIds, remoteViews); 一開始我就忘記了update , 導致widget沒有顯示
任何東西.
3.4 為GridView/ListView綁定資料集: widget中的FM電台列表是一個GridView. 那麼如何為GridView設定資料集呢.
Intent intent = new Intent(context , FMRadioWidgetService.class);intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);intent.setData(FMRadioStation.Station.CONTENT_URI);remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_gridView, intent);
設定資料集需要提供一個遠程適配器. 適配器是一個RemoteViewsService類型的對象. 該類實現一個onGetViewFactory(Intent intent)函數:
public class FMRadioWidgetService extends RemoteViewsService {private final String LOGTAG = "FMRadioWidgetService" ; private final boolean ISLOG = false ;@Overridepublic RemoteViewsFactory onGetViewFactory(Intent intent) {if(ISLOG)Log.d(LOGTAG , " FMRadioWidgetService::[onGetViewFactory ] intent = "+intent);return new FMRadioRemoteViewsFactory(getApplicationContext() , intent);}}
FMRadioRemoteViewsFactory才是一個正在的adapter. 該類實現RemoteViewsFactory介面, 並實現了onDataSetChanged() 和 getViewAt(int position)函數,
此處getViewAt(int position)的實作類別似我們傳統Adapter的bindView()函數. 然後在onDataSetChanged() 一般會從資料庫讀取資料.
@Overridepublic RemoteViews getViewAt(int position) {RemoteViews rv = new RemoteViews(mContext.getPackageName() , R.layout.widget_item);...HashMap<String, Object> map = mlistStations.get(position);rv.setTextViewText(R.id.widget_gv_txt_itme, freqStr);...Intent fillInIntent = new Intent();Bundle extras = new Bundle();extras.putInt(FMRadioAppWidgetProvider.EXTRA_SORT, position);fillInIntent.putExtras(extras);// Make it possible to distinguish the individual on-click// action of a given item rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);return rv;}@Overridepublic void onDataSetChanged() {queryFMradioStation();}
在getViewAt()中為GridView的每一個item設定click事件:
Intent fillInIntent = new Intent();
Bundle extras = new Bundle();
extras.putInt(FMRadioAppWidgetProvider.EXTRA_SORT, position);
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
而該處的intent要與FMRadioWidgetProvider.java的onUpdate函數中的:
// Set the action for the intent.
// When the user touches a particular view, it will have the effect of
// broadcasting TOAST_ACTION.
itemClcikIntent.setAction(FMRadioAppWidgetProvider.ACTION_CLICK);
itemClcikIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent itemPendingIntent = PendingIntent.getBroadcast(context, 0, itemClcikIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.widget_gridView, itemPendingIntent);
這兩處最終合成一個Intent. 當點擊每一個item的時候, 會發送一個ACTION_CLICK的廣播, 並且在FMRadioAppWidgetProvide接收廣播後, 在onReceive中進行處理.
整個widget大概基本完成.