80 lock Screen Cleanup process (focus)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.