我正在开发一个安卓应用,在其中需要展示当前运行的应用列表,但这会包括很多系统或者默认进程,例如:启动器、拨号器等。
现在有没有办法检查当前运行的进程是否为安卓系统的系统进程(默认进程)呢?
非常感谢。
以下是我的代码: 首先获取正在运行的应用程序列表,按照以下步骤操作
public void RunningApps() {
final PackageManager pm = getPackageManager();
//Get the Activity Manager Object
ActivityManager aManager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
//Get the list of running Applications
List<ActivityManager.RunningAppProcessInfo> rapInfoList =
aManager.getRunningAppProcesses();
//Iterate all running apps to get their details
for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
//error getting package name for this process so move on
if (rapInfo.pkgList.length == 0)
continue;
try {
PackageInfo pkgInfo = pm.getPackageInfo(rapInfo.pkgList[0],
PackageManager.GET_ACTIVITIES);
if (isSystemPackage(pkgInfo)) {
//do something here
}
else {
//do something here
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.d(TAG, "NameNotFoundException :" + rapInfo.pkgList[0]);
}
}
}
/**
* Return whether the given PackgeInfo represents a system package or not.
* User-installed packages (Market or otherwise) should not be denoted as
* system packages.
*
* @param pkgInfo The Package info object
* @return Boolean value indicating if the application is system app
*/
private boolean isSystemPackage(PackageInfo pkgInfo) {
return (pkgInfo.applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) != 0;
}
val rapInfoList = aManager.runningAppProcesses
这只返回应用程序进程。你知道如何获取正在运行的系统进程列表吗? - Kushal你可以使用ActivityManager来获取Android系统中所有正在运行的进程列表。关于更多信息,请参考下面的链接:Android 进程 killer。
public static boolean isAppRunning(Context context, String packageName) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
if (list.size() <= 0) {
return false;
}
for (ActivityManager.RunningTaskInfo info : list) {
if (info.baseActivity.getPackageName().equals(packageName)) {
return true;
}
}
return false;
}