如何从另一个应用程序访问字符串资源 [英] How to access String resource from another application

查看:48
本文介绍了如何从另一个应用程序访问字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序"A"和一个应用程序"B".

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

说,我在应用程序"A"中有一个字符串资源

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

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

如何从"B"中的活动"中访问abc的值.

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

我尝试了以下方法.

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.

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

关于, 萨那特

推荐答案

有可能!看一下下面的代码.它对我有用.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆