Android - How to get application name? (Not package name)

后端 未结 12 2229
天涯浪人
天涯浪人 2020-11-27 15:17

In my manifest I have:

  

        
12条回答
  •  生来不讨喜
    2020-11-27 15:37

    If not mentioned in the strings.xml/hardcoded in AndroidManifest.xml for whatever reason like android:label="MyApp"

    public String getAppLable(Context context) {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = packageManager.getApplicationInfo(context.getApplicationInfo().packageName, 0);
        } catch (final NameNotFoundException e) {
        }
        return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
    }
    

    Or if you know the String resource ID then you can directly get it via

    getString(R.string.appNameID);
    

提交回复
热议问题