赞
踩
如何获取APP的名称?网上找到的方法是:
public static String getAppName(Context context) {
if (context == null) {
return null;
}
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (Throwable e) {
Log.i(“chwn”,"getAppName >> e:" + e.toString());
}
return null;
}
该方法经过验证,发现其只能获取AndroidManifest.xml中应用名称是这样赋值的方式:
android:label="@string/app_name";
而无法兼容如下方式:
android:label="${APP_NAME}",其中APP_NAME在打包过程中会直接赋值为应用名称。
经过测试验证,如下方法才是正解:
public static String getAppName(Context context) {
if (context == null) {
return null;
}
try {
PackageManager packageManager = context.getPackageManager();
return String.valueOf(packageManager.getApplicationLabel(context.getApplicationInfo()));
} catch (Throwable e) {
Log.i(“chwn”,"getAppName >> e:" + e.toString());
}
return null;
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。