我希望使用PackageInfo将Application中的所有活动作为列表。请告诉我有没有办法做到这一点。
提前致谢。
答案 0 :(得分:15)
我的问题回答如下。
public static ArrayList<ActivityInfo> getAllRunningActivities(Context context) {
try {
PackageInfo pi = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.GET_ACTIVITIES);
return new ArrayList<>(Arrays.asList(pi.activities));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
答案 1 :(得分:1)
尝试以下代码:
final PackageManager pm = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : appList) {
Log.v("my logs", "package and activity name = "
+ temp.activityInfo.packageName + " "
+ temp.activityInfo.name);
}