【问题标题】:Android - How to get application name? (Not package name)Android - 如何获取应用程序名称? (不是包名)
【发布时间】:2012-06-27 15:00:57
【问题描述】:

在我的清单中,我有:

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

如何获取标签元素?

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    只要做:

    public static String getApplicationName(Context context) {
        ApplicationInfo applicationInfo = context.getApplicationInfo();
        int stringId = applicationInfo.labelRes;
        return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
    }
    

    希望这会有所帮助。

    编辑

    根据 Snicolas 的评论,我已经修改了上面的内容,以便它不会尝试解析 0 的 id。而是使用 nonLocalizedLabel 作为退避。无需包装在 try/catch 中。

    【讨论】:

    • 只要你真的使用android:name 中的标签,它就可以正常工作。如果你硬编码一个字符串,那么它会失败。
    • True 但应用程序名称通常是通过字符串资源指定的。硬连线字符串由 lint 标记,不建议使用。
    • 请注意,如果找不到字符串,它会抛出 android.content.res.Resources$NotFoundException
    • 按照zenocon的建议,最好把它放在try ... catch中。
    • 你知道关于 nonLocalizableLabel 的文档吗?:“...你可能想要 getApplicationLabel(ApplicationInfo)”
    【解决方案2】:

    如果在 AndroidManifest.xml 中的 strings.xml/hardcoded 中未提及,无论出于何种原因,例如 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");
    }
    

    或者如果你知道String资源ID,那么你可以直接通过

    getString(R.string.appNameID);
    

    【讨论】:

    • 从 string.xml 读取应用程序名称时,有时会收到垃圾字符。我不知道为什么..例如:स्कैनर
    【解决方案3】:

    Java

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

    Kotlin(作为扩展)

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

    【讨论】:

    • loadLabel 返回CharSequence。我想你最后需要.toString()
    • 返回是 CharSequence 或 String.valueOf();
    【解决方案4】:

    从任何上下文使用:

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

    【讨论】:

      【解决方案5】:

      如果您知道包名称,请使用以下 sn-p

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

      【讨论】:

      • @RishabhTayal pm 表示 PackageManager 对象。
      • @VipulShah 我每次都变得未知
      【解决方案6】:

      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)
              }
      

      【讨论】:

      【解决方案7】:

      在 Kotlin 中很简单:

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

      【讨论】:

      • nonLocalizedLabel.toString()
      【解决方案8】:

      如果您只需要应用程序名称,而不需要包名称,则只需编写此代码。

       String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
      

      【讨论】:

        【解决方案9】:

        使用 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
          }
        }
        

        【讨论】:

          【解决方案10】:

          好吧,伙计们,另一个时髦的选择是

          Application.Context.ApplicationInfo.NonLocalizedLabel

          已验证应用程序元素上的硬编码 android 标签。

          &lt;application android:label="Big App"&gt;&lt;/application&gt;

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

          【讨论】:

            【解决方案11】:

            添加到NonLocalizedLabel 的源评论现在将我们引导至:

            return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());
            

            【讨论】:

              【解决方案12】:

              默认情况下,您有一个由 AndroidStudio 生成的名为“app_name”的字符串资源。为什么不简单地使用它?或为此目的创建的任何其他字符串资源。比调用多个内部方法来得出一个您必须首先设置自己的值要容易得多。

              【讨论】:

                【解决方案13】:

                科特林

                一个在kotlin中获取应用名称的简单函数

                fun getApplicationName() : String{
                    var applicationName = ""
                    try {
                        val applicationInfo = application.applicationInfo
                        val stringId = applicationInfo.labelRes
                        if (stringId == 0) {
                            applicationName = applicationInfo.nonLocalizedLabel.toString()
                        }
                        else {
                            applicationName = application.getString(stringId)
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    return applicationName
                }
                

                【讨论】:

                  【解决方案14】:

                  您是否尝试过使用PackageManager#getActivityInfo() 方法?会有一个字段包含应用程序的名称。

                  查看一个非常相似的问题的答案here

                  【讨论】:

                    【解决方案15】:

                    你可以用这个

                    JAVA

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

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2017-04-24
                      • 2014-01-19
                      • 2012-04-06
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2012-01-22
                      相关资源
                      最近更新 更多