Load resource (layout) from another apk dynamically

前端 未结 3 2167
日久生厌
日久生厌 2021-02-10 02:37

I managed to pull the layouts, and i add it to my viewflipper, however, there it is loaded as blank.

The code is,

Resources packageResources;
Context pac         


        
相关标签:
3条回答
  • 2021-02-10 03:15

    I am currently doing this. It only works if a know the packageName of activity or fragment from the .apk that should provide the layout hierarchy (I call this foreign context). And you need to know the name of the layout you want to inflate (e.g. R.layout.mylayout -> "mylayout")

    Context c = createPackageContext(foreignPackageName,
        Context.CONTEXT_INCLUDE_CODE|Context.CONTEXT_IGNORE_SECURITY); //create foreign context
    int resId = c.getResources.getIdentifier(mylayoutName,"layout",foreignPackageName); 
    LayoutInflater myInflater = LayoutInflater.from(c); //Inflater for foreign context
    View myLayout = myInflater.inflate(resId,null,false); //do not attach to a root view
    
    0 讨论(0)
  • 2021-02-10 03:18

    You are not inflating a layout. You are creating an empty View and adding it to your ViewFlipper.

    0 讨论(0)
  • 2021-02-10 03:25

    Finally got it.... Its actually simple !!!!

    Here is what i did...

    1. In the target apk, there is only resources and layouts with no application or activity code. I created a class,

      public final class ViewExtractor
      {
          private static final int NUMBER_OF_LAYOUTS = 5;
      
          public static View[] getAllViews(Context context)
          {
              View[] result = new View[ViewExtractor.NUMBER_OF_LAYOUTS];
      
              result[0] = View.inflate(context, R.layout.layout_0, null);
              result[1] = View.inflate(context, R.layout.layout_1, null);
              result[2] = View.inflate(context, R.layout.layout_2, null);
              result[3] = View.inflate(context, R.layout.layout_3, null);
              result[4] = View.inflate(context, R.layout.layout_4, null);
      
              return result;
          }
       }
      

    Then in my current application, I modified my earlier code. The modification takes place once package has been verified to exist.

            // If the package exists then get the resources within it.
            // Use the method in the class to get the views.
            Class<?> viewExtractor;
            try
            {
                viewExtractor = packageContext.getClassLoader().loadClass(packageName + ".ViewExtractor");
            }
            catch (ClassNotFoundException excep)
            {
                continue;
            }
    
            View[] resultViews;
            try
            {
                Method m = viewExtractor.getDeclaredMethod("getAllViews", Context.class);
    
                resultViews= (View[])m.invoke(null, new Object[]{packageContext});
    
                for( View v : resultViews)
                {
                    this.viewFlipper.addView(v);
                }
            }
            catch (Exception excep)
            {
                excep.printStackTrace();
            }   
    
    0 讨论(0)
提交回复
热议问题