Recently in an app, there is a process management module to manage the running process in the system, and can turn off the process to speed up the function of the phone, basically implemented it out. The effect of the interface is to write their own, although there are bugs, but basically to meet the demand, later I will improve the bug. Well, let's see the effect:
1. Get the available memory and total memory of the system.
To get the information applied in the system memory, you need to use the Activitymanager class, but when you use this class to get the data you will find that the data you get is incorrect. Using this class of API to get the total memory of the system and the available memory can occur when the data is incorrect. In addition to this class, the Android phone has files describing this information-/proc/meminfo. Some data on the Android phone, including available memory and total memory, is recorded in detail in the Meminfo file. Attached code:
Public Static Longgettotalmemsize () {LongSize=0; File File=NewFile ("/proc/meminfo"); Try{BufferedReader buffer=NewBufferedReader (NewInputStreamReader (Newfileinputstream (file)); String Meminfo=Buffer.readline (); intStartIndex = Meminfo.indexof (":"); intEndIndex = Meminfo.indexof ("K"); Meminfo= meminfo.substring (StartIndex + 1, EndIndex). Trim (); Size=Long.parselong (Meminfo); Size*= 1024; Buffer.close (); } Catch(java.io.IOException e) {e.printstacktrace (); } returnsize; } Public Static Longgetaviablememsize () {LongSize=0; File File=NewFile ("/proc/meminfo"); Try{BufferedReader buffer=NewBufferedReader (NewInputStreamReader (Newfileinputstream (file)); String Meminfos=NewString (); intI=0; while((Meminfos=buffer.readline ())! =NULL) {i++; if(i==2) {Meminfo=Meminfos; } } intStartIndex = Meminfo.indexof (":"); intEndIndex = Meminfo.indexof ("K"); Meminfo= meminfo.substring (StartIndex + 1, EndIndex). Trim (); Size=Long.parselong (Meminfo); Size*= 1024; Buffer.close (); } Catch(java.io.IOException e) {e.printstacktrace (); } returnsize; }
The operation is simple to read the first row of data and the second row of data, divide the string out, the resulting value multiplied by 1024 into the byte type.
2. Get information on running apps in memory
First, it is natural to have a bean file to store this information, and then get a runningappprocessinfo list by Activitymanager's Getrunningappprocesses () method. This list is handy for removing the data we want, in our Bean folder.
Public StaticList<taskbean>Getalltask () {List<taskbean>tasklist=NewArraylist<>(); List<activitymanager.runningappprocessinfo>runlist=Uiutils.getactmanager (). getrunningappprocesses (); Try { for(Activitymanager.runningappprocessinfo r:runlist) {Taskbean Taskbean=NewTaskbean (); String ProcessName=R.processname; Taskbean.setpackagename (ProcessName); PackageInfo PackageInfo= Uiutils.getpacmanager (). Getpackageinfo (processName, 0); Taskbean.seticon (PackageInfo.applicationInfo.loadIcon (Uiutils.getpacmanager ())); Taskbean.setname (PackageInfo.applicationInfo.loadLabel (Uiutils.getpacmanager ()). ToString ()); Debug.memoryinfo[] ProcessInfo=uiutils.getactmanager (). Getprocessmemoryinfo (New int[]{r.pid}]; Taskbean.setmemsize (processinfo[0].gettotalprivatedirty () *1024); if((Packageinfo.applicationinfo.flags&applicationinfo.flag_system)!=0) {Taskbean.setsystem (true); }Else{Taskbean.setuser (true); } if(TaskList! =NULL) {tasklist.add (Taskbean); for(intI=0;i<tasklist.size (); i++) { if(Tasklist.get (i). Getpackagename (). Equals (Constants.package_info)) {tasklist.remove (i); } } } } } Catch(packagemanager.namenotfoundexception e) {e.printstacktrace (); } returntaskList; }
All right, it's done. When you are happy to get the phone on the commissioning of the time you will find that there is no data. Originally, after Android5.0, Google was completely considered to have discarded the information through the process as above. So what should we do?
Public StaticList<taskbean>Gettaskinfos () {List<AndroidAppProcess> Processinfos =processmanager.getrunningappprocesses (); List<TaskBean> TaskInfos =NewArraylist<taskbean>(); //traverse the running program and get the information in it for(androidappprocess processinfo:processinfos) {Taskbean TaskInfo=NewTaskbean (); //Package Name of the applicationString Packname =Processinfo.name; Taskinfo.setpackagename (Packname); //memory information for Lake area applicationsandroid.os.debug.memoryinfo[] Memoryinfos =Uiutils.getactmanager (). Getprocessmemoryinfo (New int[] {processinfo.pid}); LongMemsize = Memoryinfos[0].gettotalprivatedirty () * 1024L; Taskinfo.setmemsize (memsize); Taskinfo.setpackagename (Processinfo.getpackagename ()); Try { //Get application InformationApplicationInfo ApplicationInfo =Uiutils.getpacmanager (). Getapplicationinfo (Packname,0); drawable icon=Applicationinfo.loadicon (Uiutils.getpacmanager ()); Taskinfo.seticon (icon); String name=Applicationinfo.loadlabel (Uiutils.getpacmanager ()). ToString (); Taskinfo.setname (name); if((Applicationinfo.flags & applicationinfo.flag_system) = = 0) { //User ProcessTaskinfo.setuser (true); } Else { //System ProcessesTaskinfo.setsystem (true); } } Catch(packagemanager.namenotfoundexception e) {//TODO auto-generated Catch blockE.printstacktrace (); //system Kernel process has no nameTaskinfo.setname (packname); drawable icon=Uiutils.getcontext (). Getresources (). getdrawable (R.drawable.ic_launcher); Taskinfo.seticon (icon); } if(TaskInfo! =NULL) {taskinfos.add (taskinfo); for(intI=0;i<taskinfos.size (); i++) { if(Taskinfos.get (i). Getpackagename (). Equals (Constants.package_info)) {taskinfos.remove (i); } } } } returnTaskInfos; }
OK, next you just need to determine the installed version:
int sysversion == sysversion > 21? Taskmanagerengine.gettaskinfos (): Taskmanagerengine.getalltask ();
All right, it's done. The data will be properly received. On this effect, I realized the complexity, using the property animation and tweened animation, relying on material Design, the package is not very good, there are bugs so there is no way to introduce the implementation. I hope this article will be of some help to you. If you have any questions, you can leave a message below.
Android develops all process information for the system.