2010-10-08 56 views
2

我有一个应用程序'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){ 

} 

始终渣油则返回0总是..任何人都可以请让我知道,如果我可以从应用程序“B”

问候, SANAT

回答

1

似乎好了访问字符串ABC。这里是我的代码

Resources res = null; 
    try { 
     res = getPackageManager().getResourcesForApplication("com.sjm.testres1"); 
    } catch (NameNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    if(null != res) { 
     int sId = res.getIdentifier("com.sjm.testres1:string/app_name1", null, null); 
     int dId = res.getIdentifier("com.sjm.testres1:drawable/card_1_big", null, null); 
     if(0 != dId) { 
      iv.setBackgroundDrawable(res.getDrawable(dId)); 
     } 
     if(0 != sId) { 
      tv.setText(res.getString(sId)); 
     } 
    } 
6

这是可能的!看看下面的代码。这个对我有用。

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

} 

希望这会帮助你。