How to list all activities exposed by an application?

前端 未结 4 1412
鱼传尺愫
鱼传尺愫 2020-12-25 07:59

I think that it should be possible to get all the activities from \'third-party\' application, described in the manifest file. I can\'t figure out how.

for example:<

相关标签:
4条回答
  • 2020-12-25 08:28

    You should be able to do just that using the PackageManager's getPackageArchiveInfo() using the GET_ACTIVITIES flag. I have not tried it though

    0 讨论(0)
  • 2020-12-25 08:30

    If you have the application context then use this:

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-25 08:31
    public ActivityInfo[] getActivityList() throws NameNotFoundException {
        PackageManager pm = this.getPackageManager();
    
        PackageInfo info = pm.getPackageInfo(getApplicationContext.getPackageName(), PackageManager.GET_ACTIVITIES);
    
        ActivityInfo[] list = info.activities;
    
        return list;
    }
    
    0 讨论(0)
  • 2020-12-25 08:42

    Thanks for the answer!

    I think I found a solution for listing all the activities in an application too, not the most elegant though...

    //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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题