Widget就是平時所說的桌面小部件,可以很方便的在桌面上進行操作,但是本質上它是一個廣播接收器。直接看代碼。
public class TestWidget extends AppWidgetProvider{
public static final String WIDGET_BUTTON = "widget_button";
@Override
//接收廣播
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
點進AppWidgetProvider,可以看到它是繼承BroadcastReceiver的。onUpdate()在用戶把小部件拖到桌面,創建Widget實例時會調用。onReceive()在接收廣播的時候會被調用。所以在使用Widget的時候應該在AndroidManifest中注冊.。且由于當程序開始的時候就監聽,所以采用靜態注冊。
<receiver android:name=".TestWidget">
<!--將該BroadcastReceiver當成桌面控件-->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<!--指定桌面控件的meta-data-->
<meta-data android:name="android.appwidget.provider"
android:resource="@layout/widget_setting"/>
</receiver>
- action里面表示接收系統發來的有關這個app的所有widget的消息。
- meta-data 指定了對應的資源文件。name 是指定metadata名,
resource是指定對應的資源路徑。那我們就把指定的資源文件貼出來。
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:initialLayout="@layout/layout_widget"
android:minHeight="140dp"
android:minWidth="140dp"
android:previewImage="@drawable/smile"
android:updatePeriodMillis="2000"
android:widgetCategory="home_screen"
>
</appwidget-provider>
簡單解釋一下:
- initialLayout : 加載到桌面時對應的布局文件
- minWidth : 最小寬度
- minHeight : 最小高度
- previewImage : 預覽圖片
- updatePeriodMillis : 更新widget的時間間隔(ms)
- widgetCategory : widget可以被顯示的位置。home_screen表示可以將widget添加到桌面,keyguard表示widget可以被添加到鎖屏界面。
加載在桌面的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/widget_text_View"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/wighet_button"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
第一個注意的地方,widget支持的控件有限,基本上包括以下幾種:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView、AdapterView、Flipper。其他就顯示不出來
前面準備工作做的差不多了,現在就是加載布局文件了。利用RemoteViews可以直接加載整個布局文件。在onUpdate()中的代碼如下
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i(TAG, "onUpdate");
//指定加載的頁面布局文件
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
Intent intent = new Intent();
intent.setClass(context, TestWidget.class);
intent.setAction(WIDGET_BUTTON);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); remoteViews.setOnClickPendingIntent(R.id.wighet_button, pendingIntent);
//
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
為Button設置點擊事件是remoteViews.setOnClickPendingIntent(R.id.wighet_button, pendingIntent);
傳入的第一參數是Button的id,第二個是PendingIntent,,我的理解是不立即執行的意圖。現在是想點擊了Button然后發送廣播,在onReceive()中更新TextView的內容。
@Override
//接收廣播
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.i(TAG, "onReceive");
if (intent != null && TextUtils.equals(intent.getAction(), WIDGET_BUTTON)){
Log.i(WIDGET_BUTTON, "I am clicked");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
//設置文字的內容
remoteViews.setTextViewText(R.id.widget_text_View, "be clicked");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//將AppWidgetProvider的子類實例包裝成ComponentName
ComponentName componentName = new ComponentName(context, TestWidget.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
}
第二個注意的地方,RemoteViews 絕對不能復用,據說Binder data size limit is 512k,由于傳輸到appWidget進程中的Binder最大數據量是512K,并且RemoteView也不會每次清理, 所以如果每次都使用同一個RemoteView進行傳輸會因為溢出而報錯。所以必須每次重新建一個RemoteView來傳輸。
這樣一來一去,基本上就實現了一個可以操作的widget。如果想接收其他地方發的廣播,需要在AndroidManifest中注冊,然后在onReceive()中進行相關操作。
此時應該有圖,但是圖不見了,自行腦補一個TextView下有個Button。
當綁定多個Button或者view設置點擊事件的時候,如果只是設置Intent的Action不同,好像不能分辨,就是所有的按鈕都是一個功能,我的做法是將PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
的第二個參數設置的不同,這樣就可以分辨。