How to access String resource from another application

前端 未结 2 2024
逝去的感伤
逝去的感伤 2020-12-17 05:47

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

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

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


        
相关标签:
2条回答
  • 2020-12-17 05:52

    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.

    0 讨论(0)
  • 2020-12-17 06:15

    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 讨论(0)
提交回复
热议问题