Because it is not a system-level application, but also does not have root privileges, so the implementation of the Task manager itself is not very significant, like no root phone installed lbe This kind of mobile phone assistant, although also with a key to clean the memory of the background process function, but because the phone assistant does not have the highest root permissions, As a result, the process of opening daemons or other self-initiated applications is also a hands-free move. And with Google's push, is about to push the launch of the new system Android M, also on the management of more stringent authority, it is also seen a trend, the security and stability requirements for mobile systems is increasingly high.
Although the meaning is not very big, but we still come to practice practiced hand bar, perhaps one day will use it ... hahaha.
()
APIs are interfaces that provide direct access to package information, application process information, and shutdown of background processes. The idea is to get all the running processes first, including the system application process and the non-system application process. To prevent errors caused by mis-closing the system process, we filter out the system process and only show non-system processes in the list. Then, depending on the package name of the process, you can close a running process or all processes.
But here through the example, found to shut down such as the software, but soon again in the background re-create the thread continue to run, so do a good third-party mobile phone helper application, as long as the application does not get root privileges or system permissions, still can not completely shut down the process or prevent self-starting. Even if you join a lock screen automatic clearing the background, a key to clean up memory and so on, the normal application without root privileges can only be maintained by a similar daemon way, the vitality of the application, and then clear a background process every once in a while. It also consumes a little extra power. Of course this is not good, just look backstage an application can "kill" off many third-party background applications and more electricity than the original, a matter of opinion ah.
The implementation of the process, and there is no difficulty in the place, so directly on the code:
The first is the ListView item layout file Process_list_item.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" 100DP "android:padding=" 10d P "android:descendantfocusability=" blocksdescendants "> <imageview android:id=" @+id/imv_avatar " Android:layout_width= "45DP" android:layout_height= "45DP" android:layout_alignparentleft= "true" and Roid:layout_centervertical= "true" android:src= "@mipmap/ic_launcher"/> <textview android:id= "@+id/tv_app_name" android:layout_width= "wrap_content" android:layout_height= "30DP" android:gravity= " Center_vertical|left "android:layout_torightof=" @+id/imv_avatar "android:layout_centervertical=" true " android:layout_marginleft= "15DP" android:textsize= "18sp" android:text= ""/> <textview Android:id= "@+id/tv_app_process_name" Android:layout_width= "160DP" android:layout_height= "40DP" android:gravity= "center" Android:layout_c Entervertical= "true" android:textsize= "18sp" android:visibility= "Gone" android:maxlines= "2"/& Gt <button android:id= "@+id/btn_stop_app" android:layout_width= "80DP" android:layout_height= "50DP" Android:layout_alignparentright= "true" android:layout_centervertical= "true" android:text= "close Process" /></relativelayout>
Then the main class layout file Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <textview android:id= "@+id/tv_main_title" android:layout_width= "match_parent "Android:layout_height=" 50DP "android:text=" Task Manager "android:gravity=" Center "android:textsize= "18sp" android:background= "#BDBDBD" android:layout_alignparenttop= "true"/> <button a Ndroid:id= "@+id/btn_main_clear" android:layout_width= "match_parent" android:layout_height= "50DP" Android oid:text= "One-click Cleanup" android:gravity= "center" android:textsize= "16SP" android:textcolor= "#23BB67" a ndroid:layout_below= "@+id/tv_main_title"/> <listview android:id= "@+id/lv_main" Android:layo Ut_width= "Match_parent" android:layout_height= "Match_parent" android:layout_below= "@+id/btn_main_clear"/></relativelayout>
Process Information Object ProcessInfo:
/** * Created by Alextam on 2015/7/29. */public class ProcessInfo { private String labelname; Private drawable Labelicon; Private String processName; Public ProcessInfo () {} public ProcessInfo (string labelname, drawable Labelicon, String processName) { This.labelname = LabelName; This.labelicon = Labelicon; This.processname = ProcessName; } Public String Getlabelname () { return labelname; } public void Setlabelname (String labelname) { this.labelname = labelname; } Public drawable Getlabelicon () { return labelicon; } public void Setlabelicon (drawable labelicon) { This.labelicon = Labelicon; } Public String Getprocessname () { return processName; } public void Setprocessname (String processName) { this.processname = ProcessName; }}
Then there is the adapter, which adds a click event to each item's button:
/** * Created by Alextam on 2015/7/29. */public class Processlistadapter extends baseadapter{private context context; Private list<processinfo> processlist; Private Viewholder holder; Private Processlistbuttonclick listener; Public Processlistadapter (context context, list<processinfo> processlist, Processlistbuttonclick Listener ) {This.context = context; This.processlist = processlist; This.listener = listener; } @Override public int getcount () {return processlist.size (); } @Override public Object getItem (int position) {return processlist.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {ProcessInfo ProcessInfo = (ProcessInfo) GetItem (position); if (Convertview = = null) {holder = new Viewholder (); Convertview= View.inflate (context, r.layout.process_list_item, null); Holder.imv_avatar = (ImageView) Convertview.findviewbyid (R.id.imv_avatar); Holder.tv_name = (TextView) Convertview.findviewbyid (r.id.tv_app_name); Holder.tv_processname = (TextView) Convertview.findviewbyid (r.id.tv_app_process_name); HOLDER.BTN = (Button) Convertview.findviewbyid (R.id.btn_stop_app); } else {holder = (Viewholder) convertview.gettag (); } holder.imv_avatar.setImageDrawable (Processinfo.getlabelicon ()); Holder.tv_name.setText (Processinfo.getlabelname ()); Holder.tv_processName.setText (Processinfo.getprocessname ()); Holder.btn.setOnClickListener (New Ponclicklistener (Processinfo.getprocessname ())); Convertview.settag (holder); return convertview; } Private class Ponclicklistener implements View.onclicklistener {private String processName; Public Ponclicklistener (String procEssname) {this.processname = ProcessName; } @Override public void OnClick (View v) {if (listener! = NULL) listener.onb Uttonclick (ProcessName); }} Private class Viewholder {ImageView Imv_avatar; TextView Tv_name; TextView Tv_processname; Button btn; } public interface Processlistbuttonclick {void OnButtonClick (String processName); }}
OK, and finally the main class mainactivity:
/** * Created by Alextam on 2015/7/29. */public class Mainactivity extends Activity {private ListView ll_main; Private list<processinfo> processlist = new arraylist<processinfo> (); Private list<applicationinfo> applicationinfolist; Private Processlistadapter adapter = null; Private Button btn_clear; Private list<string> processnamelist = new arraylist<string> (); Private ProgressDialog ProgressDialog; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Ll_main = (ListView) Findviewbyid (R.id.lv_main); Btn_clear = (Button) Findviewbyid (r.id.btn_main_clear); Getprocesslist (); Btn_clear.setonclicklistener (New Myonclick ()); } @Override public void OnDestroy () {Super.ondestroy (); } Private class Myonclick implements View.onclicklistener {@Override public void OnClick (View v) {if (v = = btn_clear) {clearallbackgroundprocess (); }}}/** * Get a list of process information */private void Getprocesslist () {Activitymanager activity Manager = (Activitymanager) getsystemservice (Activity_service); Get all processes that will run list<activitymanager.runningappprocessinfo> runningapplist = Activitymanager.get Runningappprocesses (); Packagemanager Packagemanager = This.getpackagemanager (); Get all package Information applicationinfolist = Packagemanager.getinstalledapplications (packagemanager.get_uninstall Ed_packages); if (processlist! = null && processlist.size () > 0) processlist.clear (); if (processnamelist! = null && processnamelist.size () > 0) processnamelist.clear (); for (Activitymanager.runningappprocessinfo process:runningapplist) { if (Process.processName.indexOf (This.getpackagename ()) < 0) {//filter this app package name ProcessInfo p = n EW ProcessInfo (); ApplicationInfo appInfo = Getapplicationinfobyprocessname (process.processname); if (AppInfo = = null) {//The daemon for some apps does not have a target app counterpart and returns null} else {P.setlabelicon (Appinfo.loadicon (Packagemanager)); P.setlabelname (Appinfo.loadlabel (Packagemanager). toString ()); P.setprocessname (Appinfo.processname); Processnamelist.add (Appinfo.processname); Processlist.add (P); }}} if (adapter = = NULL) {adapter = new Processlistadapter (Mainactivity.this, Processlist,new Itembuttonclick ()); Ll_main.setadapter (adapter); Ll_main.setonitemclicklistener (New Myonitemclicklistener ()); }else {adapter.notifydatasetchanged (); }}/** * get app info based on process name * @param processnames * @return */private ApplicationInfo Getapplicationin Fobyprocessname (String processnames) {if (applicationinfolist = = NULL | | applicationinfolist.size () < 1) return null; for (ApplicationInfo applicationinfo:applicationinfolist) {if (ApplicationInfo.processName.equals (proce Ssnames) && (Applicationinfo.flags & Applicationinfo.flag_system) <= 0)/ /Show only third-party app processes, do not display system applications//To show all application processes, delete (Applicationinfo.flags & Applicationinfo.flag_system) <= 0 return applicationinfo; } return null; } Private class Myonitemclicklistener implements Adapterview.onitemclicklistener {@Override public vo ID Onitemclick (adapterview<?> parent, view view, int position, long id) {TextView TV_appname = (TextView) View.findviewbyid (r.id.tv_app_name); TextView tv_processname = (TextView) View.findviewbyid (r.id.tv_app_process_name); String appName = Tv_appname.gettext (). toString (); String processName = Tv_processname.gettext (). toString (); Toast.maketext (Mainactivity.this, "apply:" + appName + "\ n process:" + processName, Toast.length_short). Show () ; }} Private class Itembuttonclick implements Processlistadapter.processlistbuttonclick {String pName = Nu ll @Override public void OnButtonClick (String processName) {pName = ProcessName; Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.settitle ("Close process"). Setmessage ("OK to close the" + processname+ "process?") . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) {if (pName! = null) { Activitymanager Activitymanager = (activitymanager) mainactivity.this. Getsystemservice (Activity_service); Activitymanager.killbackgroundprocesses (PName); Getprocesslist (); }}). Setnegativebutton ("Cancel", new Dialoginterface.onclickli Stener () {public void OnClick (Dialoginterface dialog, int which) { Dialog.dismiss (); } }); Builder.show (); }} private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {if (Msg.what = = 0x1){Startclearanim (); } else if (msg.what = = 0x2) {Stopclearanim (); Getprocesslist (); } super.handlemessage (msg); } }; /** * One-click Cleanup */private void clearallbackgroundprocess () {mhandler.sendemptymessage (0x1); Activitymanager Activitymanager = (activitymanager) MainActivity.this.getSystemService (Activity_service); if (processnamelist! = null && processnamelist.size () > 0) {for (String PROCESSNAME:PR Ocessnamelist) {activitymanager.killbackgroundprocesses (processName); }} mhandler.sendemptymessagedelayed (0x2, 2000); } private void Startclearanim () {progressdialog = new ProgressDialog (mainactivity.this); Progressdialog.setmessage ("Trying to clean up ..."); Progressdialog.show (); } private void Stopclearanim () {ProgressDialog.dismiss (); }}
Because it is a discussion feature, one-click Cleanup does not add complex animations. If you want to join, you can consider hiding a imageview in the middle of the view, and then use Animatorset to store two property animations to achieve, one is to control imageview from transparent to full display, while the size is from 0.0f to 1.0f. The second one is to control the infinite rotation. Of course, with animation can also be achieved, the same ha. Finally, when the cleanup is complete, hide the ImageView again, set to gone. As for those who put the shortcut on the phone's desktop and then click on the shortcut component to have an animation, while cleaning the background, you can consider doing an activity, theme as a dialog, and then play the animation while cleaning the background. can achieve a similar effect.
I believe many people understand that, in fact, the Android system has its own operating mechanism, each application has its own process, each process should have a virtual machine, so designed so that when their own this process will not affect the operation of the system and other processes. When running some of the more memory-occupying applications, the system will automatically lower the priority and treat it as a "useless, outdated" background process to squeeze out memory. Of course, this is not omnipotent, because the system is not always so timely processing, so that we use such as games, such as the game, low-memory phones will still be due to the memory shortage of the reasons to feel some of the Dayton. So it seems that manual backstage has certain rationality.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Android] Implements a simple task Manager (non-root)