获取进程列表,需使用ActivityManager中的方法获取进程名;再根据进程名,获取应用程序的ApplicationInfo对象;用ApplicationInfo中的方法为进程加载图标和程序名称。获取服务、任务列表方法同上。
1.实例化ActivityManager
private ActivityManager mActivityManager;
mActivityManager=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
2 获取各列表
//获取任务列表
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()));
}
//获取服务列表
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()));
}
//获取进程列表
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对象
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实现获取应用程序相关信息列表的方法
本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性、大小和应用程序路径、应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程序,同时在获取到应用信息的时候,判断是不是系统的应用程序,这是一个应用管理器所必需具备的功能。
具体实现代码如下:
//AppInfoProvider.java
package com.xh.ui;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
/**
* 类名称:AppInfoProvider
* 类描述:获取应用程序的相关信息
* 创建人:LXH
*/
public class AppInfoProvider {
private PackageManager packageManager;
//获取一个包管理器
public AppInfoProvider(Context context){
packageManager = context.getPackageManager();
}
/**
*获取系统中所有应用信息,
*并将应用软件信息保存到list列表中。
**/
public List<AppInfo> getAllApps(){
List<AppInfo> list = new ArrayList<AppInfo>();
AppInfo myAppInfo;
//获取到所有安装了的应用程序的信息,包括那些卸载了的,但没有清除数据的应用程序
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for(PackageInfo info:packageInfos){
myAppInfo = new AppInfo();
//拿到包名
String packageName = info.packageName;
//拿到应用程序的信息
ApplicationInfo appInfo = info.applicationInfo;
//拿到应用程序的图标
Drawable icon = appInfo.loadIcon(packageManager);
//拿到应用程序的大小
//long codesize = packageStats.codeSize;
//Log.i("info", "-->"+codesize);
//拿到应用程序的程序名
String appName = appInfo.loadLabel(packageManager).toString();
myAppInfo.setPackageName(packageName);
myAppInfo.setAppName(appName);
myAppInfo.setIcon(icon);
if(filterApp(appInfo)){
myAppInfo.setSystemApp(false);
}else{
myAppInfo.setSystemApp(true);
}
list.add(myAppInfo);
}
return list;
}
/**
*判断某一个应用程序是不是系统的应用程序,
*如果是返回true,否则返回false。
*/
public boolean filterApp(ApplicationInfo info){
//有些系统应用是可以更新的,如果用户自己下载了一个系统的应用来更新了原来的,它还是系统应用,这个就是判断这种情况的
if((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
return true;
}else if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0){//判断是不是系统应用
return true;
}
return false;
}
}
与实例相关的Java类如下:(javabean)
//AppInfo.java
package com.xh.ui;
import android.graphics.drawable.Drawable;
/**
* 类名称:AppInfo
* 类描述:应用程序类,包括了程序相关属性
* 创建人:LXH
*/
public class AppInfo {
private Drawable icon;
private String appName;
private String packageName;
private boolean isSystemApp;
private long codesize;
public long getCodesize() {
return codesize;
}
public void setCodesize(long codesize) {
this.codesize = codesize;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public boolean isSystemApp() {
return isSystemApp;
}
public void setSystemApp(boolean isSystemApp) {
this.isSystemApp = isSystemApp;
}
}
package com.itheima.mobileguard.engine;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Debug.MemoryInfo;
import android.text.format.Formatter;
import com.itheima.mobileguard.R;
import com.itheima.mobileguard.domain.TaskInfo;
public class TaskInfoParser {
public static List<TaskInfo> getTaskInfos(Context context) {
PackageManager packageManager = context.getPackageManager();
List<TaskInfo> TaskInfos = new ArrayList<TaskInfo>();
// 获取到进程管理器
ActivityManager activityManager = (ActivityManager) context
.getSystemService(context.ACTIVITY_SERVICE);
// 获取到手机上面所有运行的进程
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : appProcesses) {
TaskInfo taskInfo = new TaskInfo();
// 获取到进程的名字
String processName = runningAppProcessInfo.processName;
taskInfo.setPackageName(processName);
try {
// 获取到内存基本信息
/**
* 这个里面一共只有一个数据
*/
MemoryInfo[] memoryInfo = activityManager
.getProcessMemoryInfo(new int[]{runningAppProcessInfo.pid});
// Dirty弄脏
// 获取到总共弄脏多少内存(当前应用程序占用多少内存)
int totalPrivateDirty = memoryInfo[0].getTotalPrivateDirty() * 1024;
// System.out.println("==========="+totalPrivateDirty);
taskInfo.setMemorySize(totalPrivateDirty);
PackageInfo packageInfo = packageManager.getPackageInfo(
processName, 0);
// /获取到图片
Drawable icon = packageInfo.applicationInfo
.loadIcon(packageManager);
taskInfo.setIcon(icon);
// 获取到应用的名字
String appName = packageInfo.applicationInfo.loadLabel(
packageManager).toString();
taskInfo.setAppName(appName);
System.out.println("-------------------");
System.out.println("processName="+processName);
System.out.println("appName="+appName);
//获取到当前应用程序的标记
//packageInfo.applicationInfo.flags 我们写的答案
//ApplicationInfo.FLAG_SYSTEM表示老师的该卷器
int flags = packageInfo.applicationInfo.flags;
//ApplicationInfo.FLAG_SYSTEM 表示系统应用程序
if((flags & ApplicationInfo.FLAG_SYSTEM) != 0 ){
//系统应用
taskInfo.setUserApp(false);
}else{
// /用户应用
taskInfo.setUserApp(true);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 系统核心库里面有些系统没有图标。必须给一个默认的图标
taskInfo.setAppName(processName);
taskInfo.setIcon(context.getResources().getDrawable(
R.drawable.ic_launcher));
}
TaskInfos.add(taskInfo);
}
return TaskInfos;
}
}