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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11229219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:25:07  来源:igfitidea点击:

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

android

提问by AndroidDev

In my manifest I have:

在我的清单中,我有:

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

How do I get the label element?

我如何获得标签元素?

Note: My code is running inside of someone else's, so I don't have access to @string/app_name

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

回答by darrenp

There's an easier way than the other answers that doesn't require you to name the resource explicitly or worry about exceptions with package names. It also works if you have used a string directly instead of a resource.

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

Just do:

做就是了:

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

Hope this helps.

希望这可以帮助。

Edit

编辑

In light of the comment from Snicolas, I've modified the above so that it doesn't try to resolve the id if it is 0. Instead it uses, nonLocalizedLabelas a backoff. No need for wrapping in try/catch.

根据 Snicolas 的评论,我修改了上面的内容,如果它是 0,它就不会尝试解析 id。相反,它nonLocalizedLabel用作退避。无需在 try/catch 中包装。

回答by Vinayak Bevinakatti

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

如果在 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");
}

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

或者,如果您知道 String 资源 ID,则可以直接通过

getString(R.string.appNameID);

回答by Heath Borders

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

回答by zaman

From any Context use:

从任何上下文使用:

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

回答by Vipul Shah

If you know Package name then Use following snippet

如果您知道包名称,则使用以下代码段

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

回答by Pir Fahim Shah

If you need only the application name, not the package name, then just write this code.

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

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

回答by Jerry Chong

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

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

回答by ρяσ?ρ?я K

Get Appliction Name Using RunningAppProcessInfo as:

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

回答by Asaf Pinhassi

In Kotlin its simple:

在 Kotlin 中,它很简单:

val appLabel = context.applicationInfo.nonLocalizedLabel

回答by Ahmed Alejo

Okay guys another sleek option is

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

Application.Context.ApplicationInfo.NonLocalizedLabel

Application.Context.ApplicationInfo.NonLocalizedLabel

verified for hard coded android label on application element.

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

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

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

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

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