Android 手機衛士14--Widget視窗小組件AppWidgetProvider,widget添加小組件

來源:互聯網
上載者:User

Android 手機衛士14--Widget視窗小組件AppWidgetProvider,widget添加小組件

 

1.AndroidManifest.xml根據表單小組件廣播接受者關鍵字android.appwidget.action.APPWIDGET_UPDATE
  搜尋android:resource="@xml/process_widget_provider"


2.找到xml檔案夾下process_widget_provider.xml

<appwidget-provider android:minWidth="294.0dip"     android:minHeight="72.0dip"     android:updatePeriodMillis="0"    android:initialLayout="@layout/process_widget"    xmlns:android="http://schemas.android.com/apk/res/android" />

 

3.initial Layout指向的布局檔案process_widget

 

4.表單小組件生命週期方法分析

 1 public class MyAppWidgetProvider extends AppWidgetProvider { 2     private static final String tag = "MyAppWidgetProvider"; 3     @Override 4     public void onReceive(Context context, Intent intent) { 5         Log.i(tag, "onReceive............"); 6         super.onReceive(context, intent); 7     } 8     @Override 9     public void onEnabled(Context context) {10         //建立第一個表單小組件的方法11         Log.i(tag, "onEnabled 建立第一個表單小組件調用方法");12         //開啟服務(onCreate)13         context.startService(new Intent(context, UpdateWidgetService.class));14         super.onEnabled(context);15     }16     @Override17     public void onUpdate(Context context, AppWidgetManager appWidgetManager,18             int[] appWidgetIds) {19         Log.i(tag, "onUpdate 建立多一個表單小組件調用方法");20         //開啟服務21         context.startService(new Intent(context, UpdateWidgetService.class));22         super.onUpdate(context, appWidgetManager, appWidgetIds);23     }24     @Override25     public void onAppWidgetOptionsChanged(Context context,26             AppWidgetManager appWidgetManager, int appWidgetId,27             Bundle newOptions) {28         //當表單小組件寬高發生改變的時候調用方法,建立小組件的時候,也調用此方法29         //開啟服務30         context.startService(new Intent(context, UpdateWidgetService.class));31         Log.i(tag, "onAppWidgetOptionsChanged 建立多一個表單小組件調用方法");32         super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,33                 newOptions);34     }35     36     @Override37     public void onDeleted(Context context, int[] appWidgetIds) {38         Log.i(tag, "onDeleted 刪除一個表單小組件調用方法");39         super.onDeleted(context, appWidgetIds);40     }41     42     @Override43     public void onDisabled(Context context) {44         Log.i(tag, "onDisabled 刪除最後一個表單小組件調用方法");45         //關閉服務46         context.stopService(new Intent(context, UpdateWidgetService.class));47         super.onDisabled(context);48     }49 }
MyAppWidgetProvider

5.表單小組件的更新進程總數和可用記憶體大小

    1.將更新過程放置在服務中,服務什麼時候開啟?服務什麼時候關閉?

    2.一旦出現表單小組件,則需要開啟服務,所有表單小組件銷毀的時候,關閉服務

 1 public class UpdateWidgetService extends Service { 2     protected static final String tag = "UpdateWidgetService"; 3     private Timer mTimer; 4     private InnerReceiver mInnerReceiver; 5     @Override 6     public void onCreate() { 7         //管理進程總數和可用記憶體數更新(定時器) 8         startTimer(); 9         10         //註冊開鎖,解鎖廣播接受者11         IntentFilter intentFilter = new IntentFilter();12         //開鎖action13         intentFilter.addAction(Intent.ACTION_SCREEN_ON);14         //解鎖action15         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);16         17         mInnerReceiver = new InnerReceiver();18         registerReceiver(mInnerReceiver, intentFilter);19         20         super.onCreate();21     }22     23     class InnerReceiver extends BroadcastReceiver{24         @Override25         public void onReceive(Context context, Intent intent) {26             if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){27                 //開啟定時更新任務28                 startTimer();29             }else{30                 //關閉定時更新任務31                 cancelTimerTask();32             }33         }34     }35     36     private void startTimer() {37         mTimer = new Timer();38         mTimer.scheduleAtFixedRate(new TimerTask() {39             @Override40             public void run() {41                 //ui定時重新整理42                 updateAppWidget();43                 Log.i(tag, "5秒一次的定時任務現在正在運行..........");44             }45         }, 0, 5000);46     }47     public void cancelTimerTask() {48         //mTimer中cancel方法取消定時任務方法49         if(mTimer!=null){50             mTimer.cancel();51             mTimer = null;52         }53     }54     protected void updateAppWidget() {55         //1.擷取AppWidget對象56         AppWidgetManager aWM = AppWidgetManager.getInstance(this);57         //2.擷取表單小組件布局轉換成的view對象(定位應用的包名,當前應用中的那塊布局檔案)58         RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.process_widget);59         //3.給表單小組件布view對象,內部控制項賦值60         remoteViews.setTextViewText(R.id.tv_process_count, "進程總數:"+ProcessInfoProvider.getProcessCount(this));61         //4.顯示可用記憶體大小62         String strAvailSpace = Formatter.formatFileSize(this, ProcessInfoProvider.getAvailSpace(this));63         remoteViews.setTextViewText(R.id.tv_process_memory, "可用記憶體:"+strAvailSpace);64 65         66         //點擊表單小組件,進入應用67         //1:在那個控制項上響應點擊事件2:延期的意圖68         Intent intent = new Intent("android.intent.action.HOME");69         intent.addCategory("android.intent.category.DEFAULT");70         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);71         remoteViews.setOnClickPendingIntent(R.id.ll_root, pendingIntent);72         73         //通過延期意圖發送廣播,在廣播接受者中殺死進程,匹配規則看action74         Intent broadCastintent = new Intent("android.intent.action.KILL_BACKGROUND_PROCESS");75         PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, broadCastintent, PendingIntent.FLAG_CANCEL_CURRENT);76         remoteViews.setOnClickPendingIntent(R.id.btn_clear,broadcast);77         78         //上下文環境,表單小組件對應廣播接受者的位元組碼檔案79         ComponentName componentName = new ComponentName(this,MyAppWidgetProvider.class);80         //更新表單小組件81         aWM.updateAppWidget(componentName, remoteViews);82     }83     @Override84     public IBinder onBind(Intent intent) {85         return null;86     }87     @Override88     public void onDestroy() {89         if(mInnerReceiver!=null){90             unregisterReceiver(mInnerReceiver);91         }92         //調用onDestroy即關閉服務,關閉服務的方法在移除最後一個表單小組件的時調用,定時任務也沒必要維護93         cancelTimerTask();94         super.onDestroy();95     }96 }
UpdateWidgetService

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.