如何列出应用程序公开的所有活动?

时间:2010-11-17 23:08:26

标签: android android-activity

我认为应该可以从清单文件中描述的“第三方”应用程序中获取所有活动。 我无法弄清楚如何。

例如:

List<Activity> aList = packManager.getActivitiesForPackage("package.name");

提前感谢!

3 个答案:

答案 0 :(得分:9)

您应该可以使用getPackageArchiveInfo() GET_ACTIVITIES使用{{1}}标记来执行此操作。我没试过它

答案 1 :(得分:9)

感谢您的回答!

我认为我找到了一个解决方案,用于列出应用程序中的所有活动,但不是最优雅的......

//the method gets all activities for an application
private void getAppActivities() {
    PackageManager pManager = getPackageManager();
    Intent startIntent = setStartIntent();
    List<ResolveInfo> apps = pManager.queryIntentActivities(startIntent, 0);
    int count = apps.size();
    for (int i = 0; i < count; i++) {
        ResolveInfo info = apps.get(i);
        String packageName = info.activityInfo.packageName;
        Intent intent = new Intent();
        intent.setPackage(packageName);

        //activities holds the activities defined in the package
        List<ResolveInfo> activities = pManager.queryIntentActivities(intent, 0);
    }
}

答案 2 :(得分:8)

如果您有应用程序上下文,请使用:

private static void listAllActivities(Context context) {
    PackageManager pManager = context.getPackageManager();
    String packageName = context.getApplicationContext().getPackageName();

    try {
        ActivityInfo[] list = pManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).activities;
        for (ActivityInfo activityInfo : list) {
            Log.d(TAG, "ActivityInfo = " + activityInfo.name);
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}