通过标签名称和未知的包名称在Android中打开应用程序。

时间:2014-04-05 16:03:02

标签: java android gmail lowercase

首先,我在谷歌多次搜索我的问题,但我没找到我想要的东西。

我的问题是:有没有办法通过“标签名称”在Android中打开应用程序,另一方面包名称是“知道< / strong>“?

我以前做的方法是创建两个ArrayList并将包名和标签名存储在每个中,然后,我将标签名与包名匹配后将其转换为小写以便相同字母以查看是否匹配,如果匹配,则通过获取其包名称启动应用程序。 我遇到的问题是,包名有时没有或在标签名中匹配相同的名称,基于应用程序开发人员他编写的包名称。 例如:如果我想打开gmail,根据我的方法,我不能,因为包名称是 com.google.android.gm ,标签名称是 < em> gmail ,我为延长道歉,谢谢你的进步。

我的代码:

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) {

    }
}

2 个答案:

答案 0 :(得分:2)

感谢所有回复,我通过@CommonsWare解决方案解决它并且它正常工作:

 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) {

    }
}

答案 1 :(得分:0)

试试这个

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;
}