0

I want to know the package name of an app and i only know the app name of that app. suppose i want to know the package name of an email app by just its name then how to get it
i just know the app name.

final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
     Log.d(TAG, "Installed package :" + packageInfo.packageName);
     Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}

this is the code to get the package name of all the app but i need to know for particular app.

Yurets
  • 3,999
  • 17
  • 54
  • 74
rocky
  • 1
  • 1
  • 2

2 Answers2

3

Should be something like this, since the app name is not unique you can have multiple package names that matches your requested app name.

Keep in mind that my function will return only the first package name that matches (you can change this logic very easily):

public String getPackNameByAppName(String name) {
    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> l = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    String packName = "";
    for (ApplicationInfo ai : l) {
        String n = (String)pm.getApplicationLabel(ai);
        if (n.contains(name) || name.contains(n)){
            packName = ai.packageName;
        }
    }
    
    return packName;    
}

You use TextUtils.isEmpty method to check whether you got a result or not.

Umair
  • 6,366
  • 15
  • 42
  • 50
Avi Levin
  • 1,868
  • 23
  • 32
  • It didn't open settings – rocky Jan 29 '17 at 15:09
  • packName should be initialised to what?? – rocky Jan 29 '17 at 15:32
  • "but it this code doesn't work out for many of the apps." What do you mean that it doesn't work? – Code-Apprentice Jan 29 '17 at 16:03
  • @rocky it's initialized to an empty String. Nothing goes in the quotes. This method doesn't open settings... Explain in more detail why it doesn't work – OneCricketeer Jan 29 '17 at 16:04
  • @rocky the packName is set to an empty string and would be set with a package value if there is a match in the if statement. You can use TextUtils.isEmpty method to check whether you got a result or not. – Avi Levin Jan 29 '17 at 16:04
  • @Code-Apprentice i am new to android studio and i am building an app that takes users voice as input which may be "open any app" and then open that corresponding app.. and this code doesn't open settings when i say open settings. – rocky Jan 29 '17 at 16:42
  • @rocky Since the input is from voice, you need to modify this answer to do a case-insensitive match on the application name. Or else change your voice recognition code to be smart about upper and lower case in app names. – Code-Apprentice Jan 29 '17 at 16:46
  • @Code-Apprentice can you please suggest me the code which you are telling?? – rocky Jan 30 '17 at 15:30
0

Below is the code that will help to get Package Name of all installed apps

final PackageManager pkgmanager = getPackageManager();
String TAG = "Application Info";

//Return a list of installed apps.
List<ApplicationInfo> packages =  pkgmanager.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo pkgInfo : packages) {
    Log.d(TAG, "Installed package :" + pkgInfo .packageName);
    Log.d(TAG, "Source dir : " + pkgInfo .sourceDir); 
}

Hope this will helps

Summved

Summved Jain
  • 872
  • 2
  • 18
  • 21