Android:AppWidget之案頭小電筒
安卓開發中很多控制項都是Widget類的,但是我們常說的Widget指的是AppWidget,即一些可以放置在案頭的小組件。
下面用兩個執行個體來說一下這個AppWidget怎麼來用。
執行個體一:AppWidget的基本使用
①建立一個AppWidget的布局檔案appwidget_layout.xml
②在res檔案夾下建立一個xml檔案夾,然後在裡面添加一個xml檔案appwidgetprovider_info.xml
//重新整理時間
③建立一個類繼承AppWidgetProvider,重寫幾個重要的方法
package com.example.d_appwidget;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;public class AppWidget extends AppWidgetProvider {@Overridepublic void onEnabled(Context context) { //首次添加AppWidget// TODO Auto-generated method stubsuper.onEnabled(context);System.out.println("---> onEnabled");}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager, //更新AppWidgetint[] appWidgetIds) {// TODO Auto-generated method stubsuper.onUpdate(context, appWidgetManager, appWidgetIds);System.out.println("---> onUpdate");}@Overridepublic void onDeleted(Context context, int[] appWidgetIds) { //刪除AppWidget// TODO Auto-generated method stubsuper.onDeleted(context, appWidgetIds);System.out.println("---> onDeleted");}@Overridepublic void onDisabled(Context context) { //刪除所有AppWidget// TODO Auto-generated method stubsuper.onDisabled(context);System.out.println("---> onDisabled");}@Overridepublic void onReceive(Context context, Intent intent) { //接收到廣播,比較重要的方法// TODO Auto-generated method stubsuper.onReceive(context, intent);System.out.println("---> onReceive");}}
④在AndoridManifest檔案中註冊receiver,在application節點中添加
結果:運行後可以看到視窗小組件中我們自訂的組件
執行添加刪除操作,我的理解是:執行不同操作時系統發出不同的廣播,然後執行相應的方法,執行完之後系統再次發出特定廣播。
<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+yrXA/bb+o7rU2tfAw+bJz8C0vfjQ0MrWtefNsrLZ1/ejrLrctuDK1rXnzbK2vNDo0qrPyLTyv6pBcHDIu7rz1NnIpb+qtca52LXGo6zV4tPQtePC6bezo6zL+dLU0afBy9XiuPZBcHBXaWRnZXTWrrrzztK+zdC0uPbV4rj2oaPQ6NKqy7XD97XEysejrLPM0PKxvsntus20sL/a0KGyv7z+yvTT2sG9uPa9+LPMIKOsttS/2Lz+tcTKudPDus3U2rPM0PKxvsntyrnTw8rHsrvNrLXEo6zV4tKyysfX7rjDudjXorXE0ru146Os0rK+zcrHuvPD5tC0tb21xFBlbmRpbmdJbnRlbnS6zVJlbW90ZVZpZXdzwb249sDgoaPHsMPmtcSyvdbous3KtcD90ru089bCz+DNrKOs1vfSqrK7zazKx9TavMyz0MDg1tDW2NC0tcS3vbeooaM8L3A+CjxwPqLZ0MK9qNK7uPayvL7WzsS8/mFwcHdpZGdldF9sYXlvdXQueG1sPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;"> ②res檔案夾下建立一個xml檔案夾,建立一個appwidgetprovider_info.xml檔案
③建立一個類繼承AppWidgetProvider,重寫幾個重要的方法
package com.example.e_appwidget_flashlight;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.widget.RemoteViews;public class AppWidget extends AppWidgetProvider {private static final String ACTION_TAG = "leelit.action.flash"; //待發出的廣播private static boolean flashOn = false;private static Camera camera;private static Camera.Parameters parameters;@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {// TODO Auto-generated method stubsuper.onUpdate(context, appWidgetManager, appWidgetIds);System.out.println("---> onUpdate");Intent intent = new Intent(ACTION_TAG);PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, //一定條件下才會觸發的意圖,這個意圖是發出intent, 0); //廣播 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), //獲得AppWidget的所有控制項對象R.layout.appwidget_layout);remoteViews.setOnClickPendingIntent(R.id.button, pendingIntent); //此處就能點擊觸發上面意圖,綁定為button控制項appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); //執行update,第一個參數代表組件ID,通常都是一個}@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubsuper.onReceive(context, intent);if (intent.getAction().equals(ACTION_TAG)) { //點擊後會發出一個廣播,此處就可接收RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.appwidget_layout); //因為要操作AppWidget控制項,所以要再次擷取 //remoteviews和執行相應的步驟AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context);ComponentName provider = new ComponentName(context, AppWidget.class);flashOn = !flashOn; //開啟或關閉電筒if (flashOn) {turnOnLight();remoteViews.setTextViewText(R.id.button, "電筒-關"); //改變控制項狀態appWidgetManager.updateAppWidget(provider, remoteViews);} else {turnOffLight();remoteViews.setTextViewText(R.id.button, "電筒-開");appWidgetManager.updateAppWidget(provider, remoteViews);}}}private void turnOnLight() {camera = Camera.open();parameters = camera.getParameters();parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(parameters);camera.release(); //此處必須釋放資源,否則後面關不了手電筒,因為相機資源一直在被佔據著}private void turnOffLight() {camera = Camera.open();parameters = camera.getParameters();parameters.setFlashMode(Parameters.FLASH_MODE_OFF);camera.setParameters(parameters);camera.release();}}
④在AndoridManifest檔案中註冊receiver,在application節點中添加。添加相應許可權。
結果:案頭添加一個組件,點擊操作就可以開啟或關閉手電筒了。
還有其他的一些細節就見上傳的Demo吧
AppWidget_FlashLight.rar
小結:四個步驟:①組件布局,②載入布局的xml檔案,③繼承AppWidgetProvider類,重寫方法,④配置AndoridManifest檔案