Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 手機衛士14--Widget窗口小部件AppWidgetProvider,widget添加小部件

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

編輯:關於android開發

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     @Override
17     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     @Override
25     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     @Override
37     public void onDeleted(Context context, int[] appWidgetIds) {
38         Log.i(tag, "onDeleted 刪除一個窗體小部件調用方法");
39         super.onDeleted(context, appWidgetIds);
40     }
41     
42     @Override
43     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         //開鎖action
13         intentFilter.addAction(Intent.ACTION_SCREEN_ON);
14         //解鎖action
15         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         @Override
25         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             @Override
40             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         //通過延期意圖發送廣播,在廣播接受者中殺死進程,匹配規則看action
74         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     @Override
84     public IBinder onBind(Intent intent) {
85         return null;
86     }
87     @Override
88     public void onDestroy() {
89         if(mInnerReceiver!=null){
90             unregisterReceiver(mInnerReceiver);
91         }
92         //調用onDestroy即關閉服務,關閉服務的方法在移除最後一個窗體小部件的時調用,定時任務也沒必要維護
93         cancelTimerTask();
94         super.onDestroy();
95     }
96 }
UpdateWidgetService

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved