动态类加载到目标多的Andr​​oid版本 [英] Dynamic class loading to target multiple Android versions

查看:156
本文介绍了动态类加载到目标多的Andr​​oid版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出的多个Android的版本一个Android应用程序(可能是他们每个人) 我的问题是,我要检查什么的是Android的应用程序当前正在运行的版本,并动态地加载类,取决于版本。这部分应该没问题。

I would like to make a single Android app for multiple Android versions (possibly every one of them) My problem is that I want to check what is the version of Android the app is currently running on, and dynamically load a class which is version dependent. This part should be ok.

我不知道我如何能做到这一点没有在我的Eclipse项目编译错误。 我的意思是,该项目被配置为一个特定的目标(1.5,2.1 ...),所以如果一个类在我的项目不兼容至极选定的目标,这将导致错误。

I just wonder how I can achieve that without compilation errors in my Eclipse project. I mean, the project is configured for a particular target (1.5, 2.1 ...), so if a class in my project is not compatible wich the selected target, it will result in errors.

有没有办法导出这个类,即使他们不适合的平台(我想过一个分开的lib,但话又说回来:如何编写论文类成无需编译PBS一个lib?)?这应该是好的,因为它们不会被加载,直到我让他们在已经检查Android版本。

Is there a way to export this classes even if they are not fit for the platform (I thought about a separated lib, but then again : how to compile theses classes into a lib without compilation pbs?) ? This should be ok since they won't be loaded until I ask them to after having checked Android version.

谢谢!

推荐答案

您可以使用的Class.forName 加载视不同情况不同的类:

You could use Class.forName to load different classes depending on different conditions:

public interface MyType {}
public class MyTypeOn15 implements MyType {}
public class MyTypeOn16 implements MyType {}
public class MyTypeOn20 implements MyType {}
// ...

和其他人在你的code的地方:

and somewhere else in your code:

MyType myType = null;
if (getActualTarget().equals("1.5") {
   myType = Class.forName("MyTypeOn15").newInstance();
} else if (getActualTarget().equals("1.6") {
   myType = Class.forName("MyTypeOn16").newInstance();
} // ...

请注意,你需要在你实现一个可访问空的构造。而 getActualTarget()是魔术方法的回报率目标为一个字符串标识符...

Note that you need an accessible empty constructor in your implementations. And getActualTarget() is your magic method that's returns the target as a String identifier...

这篇关于动态类加载到目标多的Andr​​oid版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆