基于SDK版本加载类 [英] Load class based on SDK version

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

问题描述

有什么办法基础上,手机运行的操作系统是什么版本,我可以加载一个类? 例如: 我做了一个应用程序需要1.6以上版本的Andr​​oid。有没有办法对我来说,加载一个类或其它基于手机运行的是什么操作系统?我问这专为联系人。 该数据库是从1.6改为2.0,旧版本不检索新的操作系统的手机的联系人。我还是想保持我的1.6的要求,但在同一时间,我想2.0+手机来访问应用程序的接触部分。

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.

所以,我可以让2的API,不知何故收拾他们的应用程序,并决定对我选择导入飞?

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.

步骤#1:创建一个接口或抽象类,描述你想在所有的API版本得到什么样的数据

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

第二步:创建具体实现/接口/抽象类的子类,每个API级别

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

第三步:在某处,发现你需要哪一个并使用它。在我的例子,我通过看在静态情况下的 android.os.Build.SDK 价值和创造我的桥梁类单身。

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天全站免登陆