The lock screen cleanup process is a useful feature that can save users a lot of power, and the lock screen cleanup process has more advantages than a timed cleanup process. Next idea:
Determine whether the user lock screen needs to register a broadcast receiver to listen, of course, this broadcast receiver in the service is better, imagine if placed in the activity, when the activity executes ondestory (), the broadcast receiver is gone. And this broadcast recipient must be the code to register.
Package Com.ustc.mobilemanager.service;import Java.util.list;import Android.app.activitymanager;import Android.app.activitymanager.runningappprocessinfo;import Android.app.service;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.ibinder;import Android.util.log;public class AutoCleanService Extends Service {public static final String TAG = "Autocleanservice";p rivate screenoffreceiver receiver;private activitym Anager am; @Overridepublic void OnCreate () {am = (Activitymanager) getsystemservice (activity_service); receiver = new Screenoffreceiver (); intentfilter filter = new Intentfilter (Intent.action_screen_off); Registerreceiver (receiver, filter); Super.oncreate ();} @Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic void OnDestroy () {unregisterreceiver (receiver); receiver = Null;super.ondestroy ();} Private class Screenoffreceiver extends broadcastreceiver{@OverridepuBlic void OnReceive (context context, Intent Intent) {log.i (TAG, "screen lock screen!"); list<runningappprocessinfo> infos = am.getrunningappprocesses (); for (Runningappprocessinfo Info:infos) { Am.killbackgroundprocesses (Info.processname);}}}
CheckBox's Click event:
Cb_auto_clean.setoncheckedchangelistener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview,boolean isChecked) {//Lock screen broadcast event is a special broadcast event that is not valid in the manifest file configuration of the broadcast recipient// The broadcast recipient of the lock screen must be registered in the code (written in the service) Intent Intent = new Intent (Tasksettingactivity.this,autocleanservice.class), if (isChecked) { StartService (intent);} else {StopService (intent);}});
The status of the checkbox is related to the lifetime of the service:
@Overrideprotected void OnStart () {Boolean running = serviceutils.isservicerunning (This, " Com.ustc.mobilemanager.service.AutoCleanService "); cb_auto_clean.setchecked (running); Super.onstart ();}
80 lock Screen Cleanup process (focus)