开发者

How to access String resource from another application

I have a application 'A' and application 'B'.

开发者_JS百科

Say, I have a string resource in the application 'A'

<\string name="abc">ABCDEF<\/string>

How do I access the value of abc from the Activity in 'B'.

I tried the following method.

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){

}

Always resId is returned 0 always.. Can anyone please let me know if I could access string abc from the application 'B'

Regards, SANAT


It is possible! Take a look at the following code. It works for me.

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

}

Hope this will help you.


It seems Ok. Here is my code

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜