如何在android中获取应用程序的名称?

时间:2011-10-18 05:27:28

标签: android

我想获取我的应用程序的名称。我怎么能得到它? 提前谢谢。

4 个答案:

答案 0 :(得分:10)

使用此

String applicationName = getResources().getString(R.string.app_name);

参考:Resource Types

答案 1 :(得分:4)

您可以使用PackageManager#getApplicationInfo()

获取设备中安装的所有软件包的应用程序名称。 假设您有当前的Context对象ctx

Resources appR = ctx.getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name",
"string", ctx.getPackageName()));

答案 2 :(得分:3)

您可以使用PackageManager课程来获取ApplicationInfo

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

编辑:CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

这将是return the application name as defined in <application> tag of its manifest

答案 3 :(得分:0)

Context具有函数getString(int resId):Context

所以你可以像这样轻松使用它。

context.getString(R.string.app_name);