在运行时从另一个应用程序获取自定义视图

发布于 2024-12-18 06:23:36 字数 670 浏览 2 评论 0原文

我正在努力思考从另一个应用程序获取 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

我希望这会起作用。我让你知道了。

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?

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 would only display somekind of animation on screen (for example falling leaves). It would not work on any data in first application.

Edit

I found something like this:

@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-12-25 06:23:36

正如我在这里所承诺的那样,这是结果。

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

使用此代码,可以使用应用程序 A 从应用程序 B 获取视图。

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

With this code it is possible to get View from application B from using application A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文