标签:lag 2.0 stat 解锁 else ges back 文件 lsp
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 }
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 }
Android 手机卫士14--Widget窗口小部件AppWidgetProvider
标签:lag 2.0 stat 解锁 else ges back 文件 lsp
原文地址:http://www.cnblogs.com/ganchuanpu/p/5986377.html