views:

90

answers:

4

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

A: 

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

Mathias Lin
its the compiling against only one sdk thats my problem, isn't it?
roman_romano
A: 

you can get the sdk version with BUILD.VERSION, check http://stackoverflow.com/questions/1882883/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.

Andy Lin
In querying the Contacts on the phone, v2.x uses ContactsContract whereas v1.x uses Contacts.ContactMethods.CONTENT_URI (am i right in think that???)Many thanks
roman_romano
yes, you are correct. that part requires checking version.here is a detail tutorial: http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/3/
Andy Lin
Many thanks for that. It's a bit above my knowledge levels but i'm going to sit down and give it a go.
roman_romano
+3  A: 

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/

Wesley Wiser
Here is a sample project using both the old and new contacts content providers, via conditional class loading: http://github.com/commonsguy/cw-advandroid/tree/master/Contacts/Spinners/
CommonsWare
A: 

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.

roman_romano