美文网首页
Get resources from another apk |

Get resources from another apk |

作者: ProgZack | 来源:发表于2016-08-22 09:26 被阅读85次

    Issue1:

    final String packName = "com.example2.app";
    String mDrawableName = "app_icon";
    
    try {
        PackageManager manager = getPackageManager();
        Resources mApk1Resources = manager.getResourcesForApplication(packName);
    
        int mDrawableResID = mApk1Resources.getIdentifier(mDrawableName, "drawable",packName);
    
        Drawable myDrawable = mApk1Resources.getDrawable( mDrawableResID );
    
        if( myDrawable != null )
            TEST.setBackgroundDrawable(myDrawable );
    
    }
    catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    Issue2:
    I have an APK that reads drawables from another of my APK.
    To do so, I have set the following fields in the manifest file for both projects:

    android:sharedUserId="com.myapp.shareduserid"
    android:process="com.myapp.process"
    

    Replace the com.myapp.shareduserid and com.myapp.process by your own values. This will allow both the APK to access data from each other.
    Once that is done you can use something similar to the sample I am providing here. Most likely you can do the same with raw. The example here uses drawables:

    mApk1Context = createPackageContext("com.apk1.app",Context.CONTEXT_IGNORE_SECURITY);
    mApk1Resources = mApk1Context.getResources();
    mDrawableResID = mApk1Resources.getIdentifier("mypng", "drawable", "com.apk1.app");
    Drawable myDrawable = mAndromedaAddonResources.getDrawable(mDrawableResID);
    if (myDrawable != null) ((ImageView) findViewById(R.id.mywindow)).setBackgroundDrawable(myDrawable);
    

    Replace com.apk1.app by your own package name.

    相关文章

      网友评论

          本文标题:Get resources from another apk |

          本文链接:https://www.haomeiwen.com/subject/nhiwsttx.html