how best to cope with different versions of Android?

谁说我不能喝 提交于 2019-11-30 07:41:07

问题


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.



来源:https://stackoverflow.com/questions/3459032/how-best-to-cope-with-different-versions-of-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!