android 获取进程、服务、任务列表

获取进程列表,需使用ActivityManager中的方法获取进程名;再根据进程名,获取应用程序的ApplicationInfo对象;用ApplicationInfo中的方法为进程加载图标和程序名称。获取服务、任务列表方法同上。   1 实例化ActivityManager
1
2
private  ActivityManager mActivityManager;
mActivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
2 获取各列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//获取任务列表
     public  void  getTasks()
     {
        aryListTaskID= new  ArrayList<String>();
        aryListTaskName= new  ArrayList<String>();
        //以getRunningTasks()取得进程TaskInfo
        List<ActivityManager.RunningTaskInfo> mRunningTasks=
            mActivityManager.getRunningTasks(intGetTaskCounter);
        for (ActivityManager.RunningTaskInfo amTask:mRunningTasks)
        {
            aryListTaskName.add(amTask.baseActivity.getClassName());
            aryListTaskID.add( "" +amTask.id);
            intTaskNum++;
        }
        tab3_tv1.setText( "任务数量:" +intTaskNum);
        tab3_tv2.setText( "可用内存:" +fileSize(getAvailableRAM()));
     }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//获取服务列表
public  void  getService()
{
    linkListServiceIcon= new  LinkedList<Drawable>();
    aryListServiceInfo= new  ArrayList<String>();
    aryListServiceName= new  ArrayList<String>();
    List<ActivityManager.RunningServiceInfo> serivces =
        mActivityManager.getRunningServices(intGetServiceCounter);
    for  (RunningServiceInfo service:serivces)
    {
        programUtil.setProcessName(service.process);
         // 根据进程名,获取应用程序的ApplicationInfo对象
         ApplicationInfo tempAppInfo = packageUtil.getApplicationInfo(service.process);
        
         // 为进程加载图标和程序名称
         if  (tempAppInfo !=  null )
         {
         linkListServiceIcon.add(tempAppInfo.loadIcon(packageManager));
         aryListServiceName.add(tempAppInfo.loadLabel(packageManager).toString());
         }
         else
         {
         linkListServiceIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.ic_launcher));
         aryListServiceName.add(service.process);
         }
       
        //MemInfo是否可得,不行则用包名代替
         if (!(mProcessMemoryUtil.getMemInfoByPid(service.pid)== "" ))
         {
         aryListServiceInfo.add(mProcessMemoryUtil.getMemInfoByPid(service.pid));
         }
         else
         {
         aryListServiceInfo.add(service.process);
         }
       
        intServiceNum++;
    }
   
    tab2_tv1.setText( "服务数量:" +intServiceNum);
    tab2_tv2.setText( "可用内存:" +fileSize(getAvailableRAM()));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
     //获取进程列表
     public  void  getProcess()
     {
        linkListProcessIcon= new  LinkedList<Drawable>();     
        aryListProcessInfo= new  ArrayList<String>();
        aryListProcessName= new  ArrayList<String>();
        
        List<RunningAppProcessInfo> apps =
            mActivityManager.getRunningAppProcesses();
        for (RunningAppProcessInfo app:apps)
        {
            //包名
//         aryListProcessName.add(app.processName);
            programUtil.setProcessName(app.processName);
             // 根据进程名,获取应用程序的ApplicationInfo对象
             ApplicationInfo tempAppInfo = packageUtil.getApplicationInfo(app.processName);
             // 为进程加载图标和程序名称
             if  (tempAppInfo !=  null )
             {
             linkListProcessIcon.add(tempAppInfo.loadIcon(packageManager));
             aryListProcessName.add(tempAppInfo.loadLabel(packageManager).toString());
             }
             else
             {
             linkListProcessIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.ic_launcher));
             aryListProcessName.add(app.processName);
             }
             //MemInfo是否可得,不行则用包名代替
             if (!(mProcessMemoryUtil.getMemInfoByPid(app.pid)== "" ))
             {
             aryListProcessInfo.add(mProcessMemoryUtil.getMemInfoByPid(app.pid));
             }
             else
             {
             aryListProcessInfo.add(app.processName);
             }
            intProcessNum++;
        }
        tab1_tv1.setText( "程序数量:" +intProcessNum);
        tab1_tv2.setText( "可用内存:" +fileSize(getAvailableRAM()));
     }
3 PackageUtil.java : 通过一个程序名返回该程序的一个ApplicationInfo对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package  com.me.myexplorer;
  
import  java.util.List;
  
import  android.content.Context;
import  android.content.pm.ApplicationInfo;
import  android.content.pm.PackageManager;
  
  
public  class  PackageUtil
{
     // ApplicationInfo 类,保存了普通应用程序的信息,主要是指Manifest.xml中application标签中的信息
     private  List<ApplicationInfo> allAppList;
    
     public  PackageUtil(Context context) {
         // 通过包管理器,检索所有的应用程序(包括卸载)与数据目录
         PackageManager pm = context.getApplicationContext().getPackageManager();
         allAppList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
         pm.getInstalledPackages( 0 );
     }
    
    
     public  ApplicationInfo getApplicationInfo(String appName) {
         if  (appName ==  null ) {
             return  null ;
         }
         for  (ApplicationInfo appinfo : allAppList) {
             if  (appName.equals(appinfo.processName)) {
                 return  appinfo;
             }
         }
         return  null ;
     }
}
 

你可能感兴趣的:(android,进程,任务,服务)