使用包名称获取所有活动

时间:2014-05-15 06:51:42

标签: android package-managers android-package-managers

我希望使用PackageInfo将Application中的所有活动作为列表。请告诉我有没有办法做到这一点。

提前致谢。

2 个答案:

答案 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);


        }