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

137

我的清单文件中有:

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

如何获取标签元素?

注意:我的代码运行在别人的代码中,所以我无法访问@string/app_name

15个回答

206

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

只需执行以下操作:

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,如果id为0,则使用nonLocalizedLabel作为后备。无需包装在try/catch中。


3
只要你在 android:name 中真正使用了标签,这个就可以正常工作。如果你硬编码了一个字符串,那么它就会失败。 - Snicolas
2
真实的,但应用程序名称通常通过字符串资源指定。硬编码的字符串会被 Lint 标记,并不建议使用。 - darrenp
1
请注意,如果找不到字符串,它会抛出android.content.res.Resources$NotFoundException异常。 - zenocon
根据zenocon的建议,最好将其放在try ... catch中。 - superarts.org
你了解nonLocalizableLabel的文档吗?:“...您可能想要使用getApplicationLabel(ApplicationInfo)” - David
nonLocalizedLabel:如果有的话,是在AndroidManifest文件中提供的字符串。您可能不想使用它,而是想使用PackageManager#getApplicationLabel。https://developer.android.com/reference/android/content/pm/PackageItemInfo#nonLocalizedLabel - David

66
如果没有在strings.xml中提到或硬编码在AndroidManifest.xml中,例如android:label="MyApp",则需要在Java代码中进行引用。
public String getAppLable(Context context) {
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = context.packageManager.getApplicationInfo(context.getPackageManager().getApplicationInfo().packageName, 0);
    } catch (final NameNotFoundException e) {
        Log.d("TAG", "The package with the given name cannot be found on the system.");
    }
    return (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}

如果您知道字符串资源的ID,那么可以直接通过以下方式获取:

getString(R.string.appNameID);

更新

Kotlin

fun getAppLable(context: Context): String? {
    var applicationInfo: ApplicationInfo? = null
    try {
        applicationInfo = context.packageManager.getApplicationInfo(context.applicationInfo.packageName, 0)
    } catch (e: NameNotFoundException) {
        Log.d("TAG", "The package with the given name cannot be found on the system.")
    }
    return (if (applicationInfo != null) packageManager.getApplicationLabel(applicationInfo) else "Unknown")
}

在从string.xml读取应用程序名称时,有时会出现垃圾字符。我不知道为什么..例如:स्कैनर - harikrishnan
它在2023年仍然有帮助。谢谢! - priyamtheone

61

Java

public static String getApplicationName(Context context) {
    return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}

Kotlin(作为扩展)

fun Context.getAppName(): String = applicationInfo.loadLabel(packageManager).toString()

3
loadLabel方法返回一个CharSequence类型的对象。我认为你需要在末尾加上.toString()方法。 - joshbodily
返回值可以是CharSequence或String.valueOf(); - Heriberto Rivera
它在2023年仍然有用。谢谢! - priyamtheone

32

从任何上下文中使用:

getApplicationInfo().loadLabel(getPackageManager()).toString();

10

Kotlin 中很简单:

val appLabel = context.applicationInfo.nonLocalizedLabel.toString()

1
nonLocalizedLabel.toString() - David

8
在 Kotlin 中,使用以下代码获取应用程序名称:
        // 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)
        }

nonLocalizedLabel:如果有的话,是在AndroidManifest文件中提供的字符串。您可能不想使用它,而是想使用PackageManager#getApplicationLabel。https://developer.android.com/reference/android/content/pm/PackageItemInfo#nonLocalizedLabel - David

7
如果您只需要应用程序名称而不是包名,那么只需编写以下代码。
 String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();

3

您可以使用此功能

JAVA

ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
String applicationLabel = getApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString();

2

如何使用RunningAppProcessInfo获取应用程序名称:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}

2

默认情况下,AndroidStudio会生成一个名为“app_name”的字符串资源。为什么不直接使用它呢?或者使用为此目的创建的任何其他字符串资源。这比调用几个内部方法来得容易,而且你需要自己设置一个值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接