Android projects get all running programs

Source: Internet
Author: User

The first is to build a JavaBean

 PackageCom.itheima.bean;Importandroid.graphics.drawable.Drawable; Public classTaskInfo {Privatedrawable icon; PrivateString name; PrivateString size; Private BooleanIsUser;  Publicdrawable GetIcon () {returnicon; }     Public voidSetIcon (drawable icon) { This. Icon =icon; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetSize () {returnsize; }     Public voidsetSize (String size) { This. Size =size; }     Public BooleanIsUser () {returnIsUser; }     Public voidSetUser (BooleanIsUser) {         This. IsUser =IsUser; } @Override PublicString toString () {return"TaskInfo [name=" + name + ", size=" +size+ ", isuser=" + IsUser + "]"; }}

Then get the list collection of the running program through Activitymanager

By iterating through the collection to get the package name,

Get additional information through Packagemanager and Baonilai

 PackageCom.itheima.engine;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.app.ActivityManager;ImportAndroid.app.ActivityManager.RunningAppProcessInfo;ImportAndroid.content.Context;ImportAndroid.content.pm.ApplicationInfo;ImportAndroid.content.pm.PackageManager;Importandroid.content.pm.PackageManager.NameNotFoundException;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Debug.MemoryInfo;ImportAndroid.text.format.Formatter;ImportCom.itheima.bean.TaskInfo;ImportCOM.ITHEIMA.SUPERMAN.R; Public classTaskinfoprovider {Private StaticString PackageName;  Public StaticList<taskinfo>Gettaskinfo (Context context) {List<TaskInfo> list =NewArraylist<taskinfo>(); Activitymanager am=(Activitymanager) context. Getsystemservice (Context.activity_service); Packagemanager pm=Context.getpackagemanager (); List<RunningAppProcessInfo> runningappprocesses =am.getrunningappprocesses ();  for(Runningappprocessinfo runningappprocessinfo:runningappprocesses) {taskinfo task=NULL; Try{Task=NewTaskInfo (); PackageName=Runningappprocessinfo.processname; //Get icondrawable LoadIcon = pm.getpackageinfo (packagename, 0). Applicationinfo.loadicon (PM);                Task.seticon (LoadIcon); //Get nameTask.setname (Pm.getpackageinfo (packagename, 0). Applicationinfo.loadlabel (PM). toString ()); //is not a user program                intFlag = Pm.getpackageinfo (PackageName, 0). Applicationinfo.flags; if((Flag&applicationinfo.flag_system) ==0) {Task.setuser (true); }Else{Task.setuser (false); }                //The program needs to consume running memoryMemoryinfo Processmemoryinfo = Am.getprocessmemoryinfo (New int[]{runningappprocessinfo.pid}] [0]; String size= Formatter.formatfilesize (context, Processmemoryinfo.gettotalprivatedirty () *1024);            Task.setsize (size); } Catch(namenotfoundexception e) {//TODO auto-generated Catch blockE.printstacktrace (); //Some programs in the system are written in C without icons and names.Task.seticon (Context.getresources (). getdrawable (R.drawable.ic_launcher));            Task.setname (PackageName);        } list.add (Task); }        returnlist; }}

Android projects get all running programs

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.