首先,让我们描述的动作,以获得标签:
- 获取
ApplicationInfo
(应用程序信息有public int labelRes
- 标签的资源ID);
- 获取应用程序资源以从中检索
labelRes
- 使用getResourcesForApplication();
- 将必要的语言环境设置为获得
Resources
并检索字符串labelRes
(请注意,我没有提及nonLocalizedLabel,在做所有上述项目之前应检查它们);
代码本身是很简单的(例如,从活动类代码):
PackageManager pm = getPackageManager();
try {
ApplicationInfo galleryInfo = pm.getApplicationInfo("com.android.gallery3d", PackageManager.GET_META_DATA);
if (null != galleryInfo) {
final String label = String.valueOf(pm.getApplicationLabel(galleryInfo));
Log.w(TAG, "Current app label is " + label);
final Configuration config = new Configuration();
config.locale = new Locale("ru");
final Resources galleryRes = pm.getResourcesForApplication("com.android.gallery3d");
galleryRes.updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
final String localizedLabel = galleryRes.getString(galleryInfo.labelRes);
Log.w(TAG, "Localized app label is " + localizedLabel);
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Failed to obtain app info!");
}
产生下面的输出(第二个字符串标签中,我从代码中设置俄语语言环境 - “RU”):
08-16 19:23:04.425: WARN/MyActivity(29122): Current app label is Gallery
08-16 19:23:04.425: WARN/MyActivity(29122): Localized app label is Галерея
是的,这就是我想要的!谢谢:) – mianlaoshu