2025-02-09

正文

  一、目標

    註意被調用的APK在Android系統中是已經安裝的。

    上篇文章:Android應用開發提高系列(4)——Android動態加載(上)——加載未安裝APK中的類

    從當前APK中調用另外一個已安裝APK的字符串、顏色值、圖片、佈局文件資源以及Activity。

  

 

二、實現
    2.1   被調用工程
       基本沿用上個工程的,添加瞭被調用的字符串、圖片等,所以這裡就不貼瞭,後面有下載工程的鏈接。
 
    2.2   調用工程代碼
 
public class TestAActivity extends Activity {

    /** TestB包名 */
    private static final String PACKAGE_TEST_B = "com.nmbb.b";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            final Context ctxTestB = getTestBContext();
            Resources res = ctxTestB.getResources();
            // 獲取字符串string
            String hello = res.getString(getId(res, "string", "hello"));
            ((TextView) findViewById(R.id.testb_string)).setText(hello);

            // 獲取圖片Drawable
            Drawable drawable = res
                    .getDrawable(getId(res, "drawable", "testb"));
            ((ImageView) findViewById(R.id.testb_drawable))
                    .setImageDrawable(drawable);

            // 獲取顏色值
            int color = res.getColor(getId(res, "color", "white"));
            ((TextView) findViewById(R.id.testb_color))
                    .setBackgroundColor(color);

            // 獲取佈局文件
            View view = getView(ctxTestB, getId(res, "layout", "main"));
            LinearLayout layout = (LinearLayout) findViewById(R.id.testb_layout);
            layout.addView(view);

            // 啟動TestB Activity
            findViewById(R.id.testb_activity).setOnClickListener(
                    new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                @SuppressWarnings("rawtypes")
                                Class cls = ctxTestB.getClassLoader()
                                        .loadClass("com.nmbb.TestBActivity");
                                startActivity(new Intent(ctxTestB, cls));
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 獲取資源對應的編號
     *
     * @param testb
     * @param resName
     * @param resType
     *            layout、drawable、string
     * @return
     */
    private int getId(Resources testb, String resType, String resName) {
        return testb.getIdentifier(resName, resType, PACKAGE_TEST_B);
    }

    /**
     * 獲取視圖
     *
     * @param ctx
     * @param id
     * @return
     */
    public View getView(Context ctx, int id) {
        return ((LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(id,
                null);
    }

    /**
     * 獲取TestB的Context
     *
     * @return
     * @throws NameNotFoundException
     */
    private Context getTestBContext() throws NameNotFoundException {
        return createPackageContext(PACKAGE_TEST_B,
                Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE); 
 
    }
    代碼說明:
      基本原理:通過package獲取被調用應用的Context,通過Context獲取相應的資源、類。
    註意:
      a).  網上許多文章是通過當前工程的R.id來調用被調用工程的資源 ,這是錯誤的,即使不報錯那也是湊巧,因為R是自動生成的,兩個應用的id是沒有辦法對應的,所以需要通過getIdentifier來查找。
      b).   Context.CONTEXT_INCLUDE_CODE一般情況下是不需要加的,如果layout裡面包含瞭自定義控件,就需要加上。註意不能在當前工程強制轉換獲得這個自定義控件,因為這是在兩個ClassLoader中,無法轉換。
      c).    獲取這些資源是不需要shareUserId的。
 
  三、總結
    與上篇文章相比,獲取資源更加方便,但也存在一些限制:
    3.1  被調用的apk必須已經安裝,降低用戶體驗。
    3.2  style是無法動態設置的,即使能夠取到。
    3.3  從目前研究結果來看,被調用工程如果使用自定義控件,會受到比較大的限制,不能強制轉換使用(原因前面已經講過)。
    3.4  由於一個工程裡面混入瞭兩個Context,比較容易造成混淆,取資源也比較麻煩。這裡分享一下批量隱射兩個apk id的辦法,可以通過反射獲取兩個apk的R類,一次獲取每一個id和值,通過名稱一一匹配上,這樣就不用手工傳入字符串瞭。
 
    @SuppressWarnings("rawtypes")
    private static HashMap<String, Integer> getR(Class cls) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        HashMap<String, Integer> result = new HashMap<String, Integer>();
        for (Class r : cls.getClasses()) {
            if (!r.getName().endsWith("styleable")) {
                Object owner = r.newInstance();
                for (Field field : r.getFields()) {
                    result.put(field.getName(), field.getInt(owner));
                }
            }
        }
        return result;
 
    }
 
  四、下載
     Test2012-4-19.zip:http://up.aiwalls.com/2012/0429/20120429100345551.zip

 
  結束

  如果是做大面積的換膚,還比較復雜,這種方式也不是很方便,這也是為什麼現在市面上做換膚的很少,有也是很簡單的換膚。這幾天想到的另外一個方案,還沒有實踐,有效果瞭再拿出來分享,歡迎大傢交流 🙂

 

摘自 農民伯伯

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *