我有一个应用程序“A”和“B”应用程序。
假设我在应用程序“A”中有一个字符串资源
<\string name="abc">ABCDEF<\/string>
如何从“B”中的活动访问 abc 的值。
我尝试了以下方法。
try {
PackageManager pm = getPackageManager();
ComponentName component = new ComponentName(
"com.android.myhome",
"com.android.myhome.WebPortalActivity");
ActivityInfo activityInfo = pm.getActivityInfo(component, 0);
Resources res = pm.getResourcesForApplication(activityInfo.applicationInfo);
int resId = res.getIdentifier("abc", "string", null);
}
catch(NameNotFoundException e){
}
resId 总是返回 0 。任何人都可以告诉我是否可以从应用程序“B”访问字符串 abc
问候,
萨纳特
有可能的!看看下面的代码。这个对我有用。
public void testUseAndroidString() {
Context context = getContext();
Resources res = null;
try {
//I want to use the clear_activities string in Package com.android.settings
res = context.getPackageManager().getResourcesForApplication("com.android.settings");
int resourceId = res.getIdentifier("com.android.settings:string/clear_activities", null, null);
if(0 != resourceId) {
CharSequence s = context.getPackageManager().getText("com.android.settings", resourceId, null);
Log.i(VIEW_LOG_TAG, "resource=" + s);
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
希望对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)