根据SDK版本加载类 [英] Load class based on SDK version

查看:153
本文介绍了根据SDK版本加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法,我可以根据手机运行的操作系统版本加载类吗?
例如:
我做了一个需要1.6+ Android的应用程序。有一种方法,我可以加载一个类或其他基于什么操作系统的手机运行?我问这个具体的联系人。
数据库从1.6更改为2.0,旧版本不会检索新操作系统电话上的联系人。我仍然希望保留我的1.6要求,但同时我想要2.0+手机访问应用程序的联系人部分。



我可以make 2 APIs,以某种方式包装他们的应用程序,并决定在飞行,我选择导入?

解决方案

是的! 这是一个示例项目,用于说明该技术。 p>

步骤1:创建一个接口或抽象类,描述您要跨所有API版本获取的数据。



步骤#2:创建接口/抽象类的具体实现/子类,每个API级别一个



步骤3:在某处,并使用它。在我的示例中,我通过查看静态上下文中的 android.os.Build.SDK 值并创建我的桥类的单例。


Is there any way I can load a class based on what version of the OS the phone is running? For example: I made an app which requires 1.6+ Android. Is there a way for me to load one class or the other based on what OS the phone is running? I'm asking this specifically for contacts. The database was changed from 1.6 to 2.0 and the old version doesn't retrieve contacts on the new OS phone. I'd still like to keep my 1.6 requirement, but at the same time I'd like 2.0+ phones to access the contact part of the app.

So can I make 2 APIs, somehow pack them with the app and decide on the fly which I choose to import?

解决方案

Yes! Here is a sample project illustrating the technique.

Step #1: Create an interface or abstract class describing what data you're trying to get across all API versions.

Step #2: Create concrete implementations/subclasses of the interface/abstract class, one for each API level.

Step #3: Somewhere, detect which one you need and use it. In my sample, I do this by looking at the android.os.Build.SDK value in a static context and creating a singleton of my "bridge" class.

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

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