tags:

views:

34

answers:

1

hi,

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

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

+1  A: 

You can't access a string resource in one application from another application.

Edit: Maybe I'm wrong, but I didn't think this was possible. Have you tried specifying a package in the 3rd parameter of getIdentifier()? I thought it was only optional because you could set it as part of the string in the first parameter, but I thought you always had to specify a package one of those 2 ways.

mbaird
@mbaird: It should be possible to do this. It's how you can get the captions for launcher icons, for example. I'm not quite certain why the OP's solution is not working, but there should be some way to accomplish this AFAIK.
CommonsWare
@CommonsWare: Doesn't the caption for launcher icons just come from the android:label attribute in the Manifest file? I don't think that even has to be defined as a resource does it? I assumed that was just a string made available to the launcher via PackageManager, PackageInfo, ActivityInfo, not via accessing a resource directly. Also, it seems like a security risk letting other apps access all your resources. Wouldn't that allow any app to copy all the resources out of my app fairly easily?
mbaird
@mbaird: " Doesn't the caption for launcher icons just come from the android:label attribute in the Manifest file?" -- which may be a reference to a string resource. "Also, it seems like a security risk letting other apps access all your resources. Wouldn't that allow any app to copy all the resources out of my app fairly easily?" -- yes. Ms. Hackborn has indicated this is possible, so, hence, I've been assuming it is possible. :-)
CommonsWare
Like I said, I didn't think it was being referenced by the Launcher as a reference to a resource, but as a string in memory via ActivityInfo or something, that may have been loaded from a resource, but not exposed to the Launcher as a reference to a resource. Maybe I'm wrong, I've never written a launcher. This question has been very educational for me. I'm bothered by the fact that any app can copy all the resources out of my app if they want. The graphics are generally much harder for me to produce than the code, if I have no option of protecting those resources it just seems wrong.
mbaird