2

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

4 Answers4

5

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
  • 8,398
  • 4
  • 40
  • 63
  • 1
    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 Aug 11 '10 at 14:36
  • http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FhsDu+%28Android+Developers+Blog%29 suggests wraping over reflections – OneWorld Dec 09 '10 at 13:41
1

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.

Community
  • 1
  • 1
Andy Lin
  • 555
  • 2
  • 4
  • 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 Aug 11 '10 at 14:42
  • 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 Aug 11 '10 at 14:57
  • 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 Aug 12 '10 at 08:19
0

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 Conradt
  • 27,904
  • 20
  • 132
  • 186
0

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.