获取有关所有正在运行的进程的信息
问题描述:
我有一个想法,可以为Android制作任务管理器.有人可以告诉我如何在Android中当前运行的所有进程吗?
I have an idea to make Task Manager for android.Can anyone tell me how to get all the processes currently running in android?
答
使用下面的代码,您可以获取正在运行的进程的列表:-
using the code below you can get the list of running processes:-
ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(RunningAppProcessInfo runningProInfo:procInfos){
Log.d("Running Processes", "()()"+runningProInfo.processName);
}
有关更多信息,您可以访问此链接.
For more information you can visit this link.
要获取基于包名称的应用程序名称,请使用 PackageManager
类.
final PackageManager pkgmgr = getApplicationContext().getPackageManager();
ApplicationInfo appinfo;
try {
appinfo = pkgmgr.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
appinfo = null;
}
final String applicationName = (String) (appinfo != null ? pkgmgr.getApplicationLabel(appinfo) : "(unknown)");
要使用PID获取应用名称,请使用:-
public static String getAppNameByPID(Context context, int pid){
ActivityManager manager
= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for(RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()){
if(processInfo.pid == pid){
return processInfo.processName;
}
}
return "";
}
最后检查应用是否为系统应用:-
and finally to check if an app is system app or not use:-
private boolean isSystemPackage(PackageInfo pkgInfo) {
return (pkgInfo.applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) != 0;
}