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

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, nonLocalizedLabel as a backoff. No need for wrapping in try/catch.

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

public static String getApplicationName(Context context) {

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

}

From any Context use:

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

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)");

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

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

Get Appliction Name Using RunningAppProcessInfo as:

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

}

}

Okay guys another sleek option is

Application.Context.ApplicationInfo.NonLocalizedLabel

verified for hard coded android label on application element.

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)

}

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.

The source comment added to NonLocalizedLabel directs us now to:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值