从当前APK中调用另外一个已安装APK的字符串、颜色值、图片、布局文件资源以及Activity

推荐图书

  • Android 2高级编程(第2版)
  • Google Android SDK开发范例大全(第2版)
  • 疯狂Android讲义


1个回答

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); 
    }