androidpackage-name

Packagename of Installed apps


I want to work on an app similar to Package Name Viewer 2.0 that could load the packagenames of all the installed apps in the device. Actually I have searched a lot about it even discussed with my seniors and even tried to contact with the owner of Package Name Viewer 2.0but got no answer. I am not asking to write the whole code just give a favor that how can i achieve this.

In short: I want to add the code in onCreate() that whenever app starts it should load all the packagenames of installed apps.

Related Question: How to get package name from anywhere?


Solution

  • Use PackageManager to retrieve ApplicationInfo of all apps.

     public static List<App> getAllAppInstalled(Context context) {
        PackageManager packageManager = context.getPackageManager();
        List<App> apps = new ArrayList<>();
        List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    
        for (ApplicationInfo packageInfo : packages) {
            String applicationName = ((packageInfo != null) ? packageManager.getApplicationLabel(packageInfo) : "Unknown").toString();
            if (packageInfo.enabled) {
                apps.add(new App(applicationName, packageInfo.packageName));
            }
        }
    
        return apps;
    }
    

    App class should look like:

    public class App {
    private String appName;
    private String appPackage;
    
    public App(String appName, String appPackage) {
        this.appName = appName;
        this.appPackage = appPackage;
    }
    //getters and setters
    }