在运行时从另一个应用程序获取customview

Jak*_*ieł 3 import android runtime class custom-view

我正在努力CustomView从另一个应用程序获取.假设我有两个应用程序A和B. CustomView例如,我知道包名称com.applicationb.CustomView.是否可以在运行时在应用程序A中创建此类的对象?

我的目标是找到一种方法来创建CustomViews并能够在我的应用程序中显示它们.但我希望他们(视图)是一个单独的apk,可以发布到Android Market,并作为某种扩展名下载.

CustomView只会在屏幕上显示一些动画(例如落叶).它不适用于第一个应用程序中的任何数据.

编辑

我找到了这样的东西:

@SuppressWarnings("rawtypes")
Class c = Class.forName(package_name);
Method m = c.getDeclaredMethod(method_name);
m.setAccessible(true);
Canvas can= (Canvas) m.invoke(null, null); // since there are no parameters, and called function is static
Run Code Online (Sandbox Code Playgroud)

我希望这会奏效.我回头告诉你.

Jak*_*ieł 5

正如我在这里所承认的那样是结果.

String packageName = "com.exapmle.sth";
String className = "com.exapmle.sth.CustomView";

String apkName = getPackageManager().getApplicationInfo(
    packageName, 0).sourceDir;
PathClassLoader myClassLoader = new dalvik.system.PathClassLoader(
    apkName, ClassLoader.getSystemClassLoader());
Class<?> handler = Class.forName(className, true, myClassLoader);
Constructor[] m = handler.getConstructors();
for (int i = 0; i < m.length; i++) {
    if (m[i].getName().contains("CustomView")) {
        animation = (View)m[i].newInstance(new Object[] { this });
        rootRL.addView(animation, new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT));
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,可以使用应用程序A从应用程序B获取View.