開發APP Widget的步驟
1、繼承AppWidgetProvider類,
public class MyWidget extends AppWidgetProvider{ <!-- 每一個Widget放置到桌面上時調用,每一次updatePeriodMillis時間到了之後也會被調用--> public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) <!-- 移除widget時調用--> public void onDeleted(Context context, int[] appWidgetIds) <!-- 不管添加多少個widget,都只會調用一次 --> public void onEnabled(Context context) <!-- 當最後一個widget從桌面刪除的時候調用--> public void onDisabled(Context context) <!-- 可用做Widget上的按鍵響應處理--> public void onReceive(Context context, Intent intent) }
一般在onUpdate中進行widget中按鍵的事件處理,和初始化值。
2、AppWidgetProvider是一個Broadcast,需要在Mainfest.xml中註冊。
<receiver android:name="繼承AppWidgetProvider的類"> <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_config"/> </receiver
3、 <meta-data>中的android:resource引用的是Widget的配置文件,需要在res/xml下新建,如果xml目錄不存在,也需要新建。
widget_config文件
<?xml version="1.0" encoding="UTF-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="40dp" <!-- 顯示在桌面上最小的的寬,高,不能大於4*4的格子--> android:minHeight="20dp" android:updatePeriodMillis="1000000" <!-- widget的更新間隔時間--> android:previewImage="@drawable/l7" <!-- 預覽圖--> android:initialLayout="@layout/my_widgets_layout" <!-- 在桌面的顯示佈局,在3.0版本之後可以使用,默認以應用圖標顯示。--> android:resizeMode="horizontal|vertical" <!-- 在桌面可調整的方向--> android:configure="cn.fly.widgets.MyWidgetsConfigActivity"> <!-- 當widget在拖到桌面上時,顯示的配置文件--> <!--加上以下配置,在4.2版本可以放到鎖屏界面上,4.2以下的版本默認是放主屏幕 android:widgetCategory ="home_screen|keyguard" android:initialKeyguardLayout="" 在鎖屏界面上顯示的佈局 --> </appwidget-provider>
設置了updatePeriodMillis後,就算手機處於待機狀態,也會喚醒手機去執行更新操作,如果widget更新頻繁,這會對手機電池壽命造成影響,所以針對這種情況,可以使用AlarmManager來執行定時更新操作,將AlarmManager的Type設置爲 ELAPSED_REALTIME 或者 RTC,就可以保證手機在喚醒的狀態下執行更新,updatePeriodMillis則需要設置爲0。
在桌面顯示的佈局文件的跟節點必須是常用佈局例如:Framelayout,linearLayout,viewStub等,不能使用自定義佈局。
在4.0以後的版本,widget放置到桌面上是可以根據其他的widget或者快捷圖標來擴充邊距,使添加的widget能夠在桌面視覺上達到協調,但是4.0以下的版本需要自己來設置Padding,使widget能和其他桌面圖標達到視覺協調。
做完以上的操作,一個簡單的widget的就完成了,根據應用需求我們會遇到以下的一些情況。
一、相應widget的按鍵事件
因爲widget不是運行在自身的應用的進程中,所以是通過以下方式來處理事件。
RemoteView 進行佈局引用。
PendingIntent 進行事件分發。
//這段代碼一般寫在onUpdate中,所處理的事情是按button更改Textview的顯示 final int N = appWidgetIds.length; for(int i = 0;i< N;i++){ Intent intent = new Intent("自定義action用來標示這個intent"); intent.setClass(context, MyWidgets.class); //發送廣播,在onReceive()中進行處理PendingIntent也可以啓動Activity,也可以//啓動Service PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), "widget佈局引用"); //button按鍵處理 remoteViews.setOnClickPendingIntent(R.id.btn_click, mPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); }
在onReceive()的處理
String action = intent.getAction(); if(action.equals("比較發送的action")){ RemoteViews remoteViews = new RemoteViews(context.getPackageName(),"widget佈局引用"); //更改文本顯示 remoteViews.setTextViewText(R.id.tv, "測試"); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName componentName = new ComponentName(context,MyWidgets.class); appWidgetManager.updateAppWidget(componentName, remoteViews); }
二、widget添加上桌面上時的配置頁面
這個配置頁面是一個activity,所以需要在mainfest.xml中進行配置
1、在啓動這個配置頁面的時候,需要在oncreate()方法裏面寫上setResult(RESULT_CANCELED);這樣做是爲了防止配置頁面還沒有顯示出來,用戶就按了返回鍵,
setResult(RESULT_CANCELED); super.onCreate(savedInstanceState);
2、需要獲取widgetId
Intent intent = getIntent(); Bundle extra = intent.getExtras(); if(extra !=null){ mAppWidgetId = extra.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); }
如果mAppWidgetId爲INVALID_APPWIDGET_ID則退出頁面顯示。
3、當配置頁面顯示後,用戶做完了配置退出頁面時,需要將這些配置信息更新到桌面的widget,照着官方給出的文檔上來操作的話,只能顯示widget但是配置並沒有生效,參考了appdemo中的例子和個人調試後總結如下。
在MyWidget中寫一個靜態方法
//describe是一個在配置文件中自由輸入文本,然後在widget的text上顯示,widget是爲了做到代碼重用,這裏只是一個簡單的更改文本,如果涉及到圖片更改,列表數據改動等,這些改動如果有配置界面的話基本上和onupdate所做的操作一樣。 public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId, String describe) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); views.setTextViewText(R.id.tv, describe); appWidgetManager.updateAppWidget(appWidgetId, views); }
在配置界面的退出方法中這樣寫
private void exits(){ String describe = et.getText().toString(); Context context = this; AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); //appWidgetId在進入配置界面的時候已經獲取了 MyWidget.updateAppWidget(context,appWidgetManager,appWidgetId , describe ); //配置完成退出界面 Intent intent = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); setResult(RESULT_OK, intent ); finish(); }
當時參考文檔做時,文檔中並沒有提到調用updateAppWidget方法後,退出界面需要重新獲取view,並改變view的顯示數據。所以我只寫了appWidgetManager.updateAppWidget(appWidgetId, views)方法。