问题描述
我从另一个应用程序获得 CustomView
的思想挣扎。可以说我有两个应用程序A和B,我知道的包名 CustomView
,例如 com.applicationb.CustomView
。是否有可能在运行时创建该类应用A的对象?
I am struggling with the idea of getting CustomView
from another application. Lets say I have two applications A and B. I know package name of CustomView
, for example com.applicationb.CustomView
. Is it possible to create an Object of this Class in application A at runtime?
我的目标是找到一种方法来创建 CustomViews
并能够向他们展示我的应用程序。但我希望他们(的意见)是一个单独的APK,它可以被发布到Android电子市场,并下载为某种延伸。
My aim is to find a way to create CustomViews
and being able to show them in my application. But I want them (views) to be a separate apk, which may be published to Android Market, and downloaded as some kind of extension.
CustomView
将只显示somekind的动画在屏幕上(例如落叶)。它不会在第一次申请的任何数据。
CustomView
would only display somekind of animation on screen (for example falling leaves). It would not work on any data in first application.
我发现了这样的事情:
@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
我希望这会工作。我让你知道。
I hope this will work. I let you know.
推荐答案
当我promissed这里是结果。
As I promissed here is result.
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));
}
}
有了这个code,可以使用应用程序A从应用程序B获取视图。
With this code it is possible to get View from application B from using application A.
这篇关于获取customview在运行时从另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!