在小控件广播中不能执行耗时操作,因为生命周期只有10s,可以开启一个service执行耗时操作
<span style="font-size:18px;">public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent =new Intent(context,KillProcesWidgetService.class);
context.startService(intent);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Intent intent =new Intent(context,KillProcesWidgetService.class);
context.stopService(intent);
}
}</span>
下面是由控件广播开启的清理进程的服务
<span style="font-size:18px;">public class KillProcesWidgetService extends Service {
private AppWidgetManager widgetManager;
private Timer timer;
private TimerTask task;
public KillProcesWidgetService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
//得到桌面小控件的管理者
<span style="background-color: rgb(204, 204, 204);"> widgetManager = AppWidgetManager.getInstance(this);
</span> timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
System.out.println("1111111111111111");
//在别的应用中获取view 使用远程view
RemoteViews views = new RemoteViews(getPackageName(), R.layout.process_widget);
/**
* 需要注意。这个里面没有findingviewyid这个方法
* 设置当前文本里面一共有多少个进程
*/
int processCount = SystemInfoUtils.getProcessCount(getApplicationContext());
//设置文本
views.setTextViewText(R.id.process_count, "正在运行的软件:" + String.valueOf
(processCount));
//获取到当前手机上面的可用内存
long availMem = SystemInfoUtils.getAvailMem(getApplicationContext());
views.setTextViewText(R.id.process_memory, "可用内存:" + Formatter.formatFileSize
(getApplicationContext(), availMem));
Intent intent = new Intent();
//不能使用显示意图,发送一个隐式意图
intent.setAction("com.du.safeapp");
//需要一个延迟意图发送广播来完成相应操作
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//设置点击事件
views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent);
//第一个参数表示上下文
//第二个参数表示当前有哪一个广播进行去处理当前的桌面小控件
ComponentName provider = new ComponentName(getApplicationContext(),
AppWidgetProvider.class);
<span style="color:#330033;background-color: rgb(204, 204, 204);"> widgetManager.updateAppWidget(provider, views);
</span>
}
};
timer.schedule(task, 0, 5000);
}
@Override
public void onDestroy() {
//优化代码
if(timer != null || task != null){
timer.cancel();
task.cancel();
timer = null;
task = null;
}
}
}</span>
<span style="font-size:18px;">
</span>
在服务里是使用隐式意图发送一个清理进程的广播
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">public class KillProcessAllReceiver extends BroadcastReceiver {
public KillProcessAllReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context
.ACTIVITY_SERVICE);
//得到手机上面正在运行的进程
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : appProcesses) {
//杀死所有的进程
activityManager.killBackgroundProcesses(runningAppProcessInfo.processName);
}
Toast.makeText(context, "清理完毕", 0).show();
}
}</span>