如何以编程方式获取所有正在运行的应用程序的列表

时间:2019-11-19 06:40:40

标签: java android

我正在尝试此方法,但已过时。

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

for (int i = 0; i < tasks.size(); i++) {
    Log.e("Running task", "Running task: " + tasks.get(i).baseActivity.toShortString() + "\t\t ID: " + tasks.get(i).id);
}

2 个答案:

答案 0 :(得分:0)

这是获取所有已安装应用程序列表的简便方法

 final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = 
pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
Log.d(TAG, "Launch Activity :" + 
pm.getLaunchIntentForPackage(packageInfo.packageName)); 
}

答案 1 :(得分:-1)

您可以尝试以下方法:

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = 
context.getPackageManager().queryIntentActivities( mainIntent, 0);