我有一个应用程序'A'和应用程序'B'.
说,我在应用程序'A'中有一个字符串资源
<\string name="abc">ABCDEF<\/string>
Run Code Online (Sandbox Code Playgroud)
如何从'B'中的Activity访问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){
}
Run Code Online (Sandbox Code Playgroud)
始终resId总是返回0 ..任何人都可以告诉我是否可以从应用程序'B'访问字符串abc
此致,SANAT
有可能的!看看下面的代码.这个对我有用.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
| 归档时间: |
|
| 查看次数: |
4890 次 |
| 最近记录: |