Android– 如何获取应用程序名称? (不是包名)

Android– 如何获取应用程序名称? (不是包名),第1张

概述在我的清单中我有:<applicationandroid:name=".MyApp"android:icon="@drawable/ic_launcher_icon"android:label="@string/app_name"android:debuggable="true">我如何获得标签元素?注意:我的代码在别人的内部运行,因此我无法访问@string

在我的清单中我有:

  <application    androID:name=".MyApp"    androID:icon="@drawable/ic_launcher_icon"    androID:label="@string/app_name"    androID:deBUGgable="true">

我如何获得标签元素?

注意:我的代码在别人的内部运行,因此我无法访问@ string / app_name

解决方法:

有一种比其他答案更简单的方法,它不需要您明确命名资源或担心包名称的异常.如果您直接使用字符串而不是资源,它也可以工作.

做就是了:

public static String getApplicationname(Context context) {    ApplicationInfo applicationInfo = context.getApplicationInfo();    int stringID = applicationInfo.labelRes;    return stringID == 0 ? applicationInfo.nonlocalizedLabel.toString() : context.getString(stringID);}

希望这可以帮助.

编辑

根据Snicolas的评论,我修改了上面的内容,以便它不会尝试解析ID,如果它是0.相反,它使用nonlocalizedLabel作为退避.不需要包装try / catch.

总结

以上是内存溢出为你收集整理的Android – 如何获取应用程序名称? (不是包名)全部内容,希望文章能够帮你解决Android – 如何获取应用程序名称? (不是包名)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103584.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存