开发者

Getting resources of another Application

Assume I've got 2 Application A and B.

I want to access resources (drawables, images, strings) of an B application from A application. How would I do that?

Edit:

You can also designate an Android project as a library project, which allows it to be shared with other projects that depend on it. Once an Android project is designated as a library project, it 开发者_Go百科cannot be installed onto a device.

Does it mean That I can not use my library on android market? My aim is to use default resources of Application A, but also if someone what he can download Application B and use it resources.


If you are the developer of both applications then the right approach would be, as othes noted, to create an Android library project and share it between apps.

Nevertheles, you can still access resources from another app, even if this is not your app:

Resources res = context.getPackageManager().getResourcesForApplication("com.example.foo")


If both apps are your own and you want to bundle these resources at build-time:

Create an Android Library Project, put all the needed resources into it and reference it from both your apps.


Edit to your edit: You can use the library in the android market. You just can not use it alone (compile it). You have to build an app that uses the library. When you build that app, all the needed content gets basically grabbed from the library and copied to your apk. Which you can put on the market as usual.

This does not work when you want to access resources from an app that gets downloaded onto the device at runtime. A library project bundles all resources when you build the app that uses it. Peter Knego's response is the correct one when accessing at runtime.


It is possible if those both application are yours. Or you can used any Android Library for both these application. If you want you also create your own Android Library for this kind of work. Thnx.


Peter's answer above is great, but I'd like to elaborate a little more...

When you control appA and appB and want to get a drawable from appB, while in appA, then do the following:

  1. Get the identifier of the drawable when in appB (for example, R.drawable.iconInAppB)
  2. Then, in appA

    Resources res = context.getPackageManager().getResourcesForApplication("com.example.appB");
    
        Drawable d = res.getDrawable(integer);
    //integer was what appB assigned to R.drawable.iconInAppB
    

*Note: The integer id assigned to R.drawable.iconInAppB will change over time so don't rely on hardcoding. I save the key-value pairs (String resourceName, int resourceId) in a database in appB and access them using a content provider.


For example, let's get the string-array with the ID of entryvalues_font_size from the package com.android.settings

String[] enries = null;
try {
    // You can get PackageManager from the Context
    Resources res = getPackageManager().getResourcesForApplication("com.android.settings");
    int resId = res.getIdentifier("entryvalues_font_size", "array", "com.android.settings");
    if (resId != 0) {
        enries = res.getStringArray(resId);
    }
}
catch (NameNotFoundException e) {
    e.printStackTrace();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜