how best to cope with different versions of Androi

2019-01-24 23:05发布

问题:

I have an app that works fine in Android 1.x but not in Android 2.x i need to do things different based on the version of Android the app is running on (querying contacts). is it possible to have two separate methods within the one app that i can choose based on the version of Android the app is running on?

many thanks

Ed

回答1:

Use reflection and class loaders. See this post on the Android developers blog: http://feedproxy.google.com/~r/blogspot/hsDu/~3/9WEwRp2NWlY/how-to-have-your-cupcake-and-eat-it-too.html

Edit: Thanks to CommonsWare for pointing out a sample project which uses both the new and old contacts content providers and conditional class loading: http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Spinners/



回答2:

you can get the sdk version with BUILD.VERSION, check How to retrieve the android sdk version?

however, i am wondering what function is runnable on 1.x and not available on 2.x. did you use any of the internal classes?

I really suggest that you fix the function issue, rather than doing different things with different versions, if it can be avoid.



回答3:

You can read out the OS version of the device.You can have different methods in your app depending on the device OS version, but you can only compile against one SDK version. Therefore, you need to choose the minimum, which at the moment currently is 1.6 (I think 1.5 is rarely used anymore)

see: http://developer.android.com/reference/android/os/Build.VERSION.html

developing for multiple screens / devices: http://developer.android.com/guide/practices/screens_support.html



回答4:

The Business Card example that comes with the latest Android SDK was a big, big help. I recommend it to those that like me might not be a professional full-time developer. Thanks everyone for your kind input.