当前位置:操作系统 > 安卓/Android >>

Android应用开发提高系列(5)——Android动态加载(下)——加载已安装APK中的类和资源

正文

  一、目标

    注意被调用的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 NameNotFo

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,