Get package name of another app by app name

自闭症网瘾萝莉.ら 提交于 2019-12-25 09:02:35

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/41918056/get-package-name-of-another-app-by-app-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!