2

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?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Thomas
  • 121
  • 2
  • 12

1 Answers1

0

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;

}
Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30
  • I need to get the packge name of the string (which is the app name) returned by parse. If I try the abpve code, i am getting the package name of my own app which I dont need. What should I change? – Thomas Oct 05 '17 at 10:13
  • whta you want then – Vishal Yadav Oct 05 '17 at 10:27
  • In my code above, the Name function returns a string. I need the package name of that – Thomas Oct 05 '17 at 10:28
  • you want the `package` of installed appps – Vishal Yadav Oct 05 '17 at 10:33
  • I want the package name of the string. For example, if the string value is instagram, then I need the package name of that – Thomas Oct 05 '17 at 10:36
  • @Thomas. Feels like guess work. What if there are many apps with the same name? Can you not use the package name directly, instead of the name then trying to figure out the package name from it – IAmGroot Oct 05 '17 at 10:42
  • @Doomsknight. Yes true. But when any app is trigerring permission request, then I am extracting the app name from the permission window. How can I extract the package name directly from that? – Thomas Oct 05 '17 at 10:45
  • @Thomas Its not something im familiar with sorry. Just making an observation or possible ideas. – IAmGroot Oct 05 '17 at 10:47