如何使用包名称获取应用程序名称(假设我已经拥有包名称)

时间:2017-04-13 20:10:31

标签: java android

请我将android代码转换为应用程序名称(假设我已经有了包名,例如String name =" com.skgames.trafficrider")

2 个答案:

答案 0 :(得分:0)

将包名称添加到此函数并获取AppName(应用程序标签)

 public String appName(String pack){
        String Name = null;

        try{
            PackageManager packManager = context.getPackageManager();
            ApplicationInfo app = context.getPackageManager().getApplicationInfo(pack, 0);
            Name = packManager.getApplicationLabel(app).toString();
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return Name;
    }

答案 1 :(得分:0)

尝试使用下一个代码:

ApplicationInfo ai;
try {
    ai = pm.getApplicationInfo("com.skgames.trafficrider", 0);
} catch (final NameNotFoundException e) {
    ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");