1. 获取当前应用进程信息:
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo info : manager.getRunningAppProcesses()){
Log.e("ProcessUtil", "processName=" + info.processName +
" uid=" + info.uid + " pid=" + info.pid
+ " pkgList=" + Arrays.toString(info.pkgList));
}
说明:
至安卓5.0(api-21)开始,getRunningAppProcesses()就只能获取应用自己进程信息
可通过获取 pkgList 信息来获取当前应用所运行的所有包名
2.获取所有第三方正在运行的进程信息:
PackageManager pm = context.getPackageManager();
for (PackageInfo lp : pm.getInstalledPackages(0)){
if (((ApplicationInfo.FLAG_SYSTEM & lp.applicationInfo.flags) == 0)
&& ((ApplicationInfo.FLAG_UPDATED_SYSTEM_APP & lp.applicationInfo.flags) == 0)
&& ((ApplicationInfo.FLAG_STOPPED & lp.applicationInfo.flags) == 0)) {
// 第三方正在运行的 app 进程信息 更多信息查询 PackageInfo 类
Log.e("ProcessUtil", "packageName=" + lp.packageName
+ " processName=" + lp.applicationInfo.processName);
}
}
说明:
a. (ApplicationInfo.FLAG_SYSTEM & lp.applicationInfo.flags):值为 1 则为系统应用, 0 则为第三方应用
b.??(ApplicationInfo.FLAG_UPDATED_SYSTEM_APP & lp.applicationInfo.flags): 值为 1 则为系统经更新后变为第三方的应用, 0 则为其他应用
c.? (ApplicationInfo.FLAG_STOPPED & lp.applicationInfo.flags): 值为 1 则此应用程序的包处于停止状态, 0 则此应用程序处于运行状态
|