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

后端 未结 12 2171
天涯浪人
天涯浪人 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);
    
    0 讨论(0)
  • 2020-11-27 15:38

    In Kotlin its simple:

    val appLabel = context.applicationInfo.nonLocalizedLabel
    
    0 讨论(0)
  • 2020-11-27 15:42
    public static String getApplicationName(Context context) {
        return context.getApplicationInfo().loadLabel(context.getPackageManager());
    }
    
    0 讨论(0)
  • 2020-11-27 15:45

    In Kotlin, use the following codes to get Application Name:

            // Get App Name
            var appName: String = ""
            val applicationInfo = this.getApplicationInfo()
            val stringId = applicationInfo.labelRes
            if (stringId == 0) {
                appName = applicationInfo.nonLocalizedLabel.toString()
            }
            else {
                appName = this.getString(stringId)
            }
    
    0 讨论(0)
  • 2020-11-27 15:46

    Okay guys another sleek option is

    Application.Context.ApplicationInfo.NonLocalizedLabel

    verified for hard coded android label on application element.

    <application android:label="Big App"></application>

    Reference: http://developer.android.com/reference/android/content/pm/PackageItemInfo.html#nonLocalizedLabel

    0 讨论(0)
  • 2020-11-27 15:51

    Have you tried using the PackageManager#getActivityInfo() method? There will be a field that should contain the name of the application.

    See the answer to a very similar question here.

    0 讨论(0)
提交回复
热议问题