Get the package name of the app

Viewed 720

So I am making an app in which I needed some features. I am getting the package name of all installed apps on my android device using this code:

final PackageManager manager = getPackageManager();

List<ApplicationInfo> packages = manager.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo info : packages) {
  Log.i("Info", "Installed package:" + info.packageName);
}

Next, whenever my app is active on the Android device, if any other app triggers any kind of permission request then I am retrieving the name of that app from the permission window and displaying it in my logcat.

I am getting the name of the app in this case. Is there any way that I could get the package name of the app?

1 Answers

Try this to get package name of the app


 List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);

ArrayList<AppInfo> res = new ArrayList<AppInfo>();
for(int i=0;i<apps.size();i++) {
                PackageInfo p = apps.get(i);

                AppInfo newInfo = new AppInfo();
                newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
                newInfo.pname = p.packageName;
                newInfo.versionName = p.versionName;
                newInfo.versionCode = p.versionCode;
                newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
                res.add(newInfo);
                }
            }

class AppInfo {
    String appname = "";
    String pname = "";
    String versionName = "";
    int versionCode = 0;
    Drawable icon;

}
Related