Question

First of all, I searching for my question many times in Google but I didn't find what I want.

My Question is: Is there any way to open application in Android by "label name" where on the other hand the package name is "knowing" ?

My approach that I used to do this is by creating two ArrayList and store the package name and label name in each one, then, I matching the label name with package name after I converted them to lowercase in order to be the same letters to see if there are matching, if matching , then I launch the application by getting its package name. the problem that I faced it, the package name sometimes didn't have or matching the same name in label name, based on the application developer what he had write the package name. for example: if I want to open gmail , based on my method I cannot because the package name is com.google.android.gm and label name is gmail , I apologized to the prolongation ,thanks in advanced.

My code:

private void getAllApps() {
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> activities = getPackageManager()
            .queryIntentActivities(mainIntent, 0);
    for (ResolveInfo resolveInfo : activities) {
        appsName.add(resolveInfo.loadLabel(getPackageManager()).toString()
                .toLowerCase());
        pkgsName.add(resolveInfo.activityInfo.packageName.toString());
    }
}

private void openApplication(String appName) {

    String packageName = null, appNameLowerCase = null, pkgNameLowerCase = null;

    // matching the package name with label name
    if (appsName.contains(appName)) {
        appNameLowerCase = appName.trim().replace(" ", "");
        for (int i = 0; i < pkgsName.size(); i++) {
            pkgNameLowerCase = pkgsName.get(i).trim().toLowerCase();
            if (pkgNameLowerCase
                    .matches("(.*)" + appNameLowerCase + "(.*)")) {
                packageName = pkgsName.get(i);
                break;
            }
        }
    }

    // to launch the application
    Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage(packageName);
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {

    }
}
Was it helpful?

Solution

thanks for all replies, i solve it by @CommonsWare solution and it works correctly :

 class Applications {
    private String packageName;
    private String labelName;
 }

 private void getAllApps() {
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> activities = getPackageManager()
            .queryIntentActivities(mainIntent, 0);
    for (ResolveInfo resolveInfo : activities) {
        Applications applications = new Applications();
        applications.labelName = resolveInfo.loadLabel(getPackageManager())
                .toString().toLowerCase();
        applications.packageName = resolveInfo.activityInfo.packageName
                .toString();
        applicationsArrayList.add(applications);
    }
}

private void openApplication(String appName) {

    String packageName = null;

    // matching the package name with label name
    for (int i = 0; i < applicationsArrayList.size(); i++) {
        if (applicationsArrayList.get(i).labelName.trim().equals(
                appName.trim())) {
            packageName = applicationsArrayList.get(i).packageName;
            break;
        }
    }

    // to launch the application
    Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage(packageName);
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {

    }
}

OTHER TIPS

Try this

public List<ApplicationInfo> getApplicationList(Context con){
    PackageManager p = con.getPackageManager(); 
    List<ApplicationInfo> info = p.getInstalledApplications(0);
    String example = info.get(0).packageName.toString();
    return info;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top