4

I am pretty sure this question has been asked before, but I could not find it anywhere, so please don't bite.

I am writing an android app, that needs to communicate with nearby devices and I want to used WifiDirect API if two devices that have it happen to be nearby.

But if that's not the case application can still work and it will use other less effective ways to communicate between participating devices (like Wifi Access Point sharing).

I also want this app to be possible to run on older devices like android 2.2 which does not have WifiDirect API at all (>=4.0 I think).

So is there anyway to make my app optionally use new API, but not necessarily in case of older phones?

siemanko
  • 1,389
  • 1
  • 13
  • 26
  • 1
    Are you talking about the Android API autoswitching for you? If not, I think you can switch implementaion with `if` statements based on the SDK/API version. – A--C Dec 19 '12 at 00:14
  • 1
    FYI, you can see an example of what @A--C is talking about in the related questions, e.g. this one: http://stackoverflow.com/questions/4236902/android-api-version-compatibility?rq=1 – kabuko Dec 19 '12 at 00:17
  • Perfect that is exactly what I wanted! – siemanko Dec 19 '12 at 12:29

2 Answers2

9

so please don't bite

Can we at least gnaw a little? Just a nibble?

:-)

So is there anyway to make my app optionally use new API, but not necessarily in case of older phones?

You can wrap your references to new classes/methods in Java version guard blocks:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  // do stuff with WifiP2pManager
}
// optional else block for workaround for older devices

There may be other particulars for WiFiDirect, which I haven't used yet, but that's the basic step.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    One interesting [answer](http://stackoverflow.com/a/6121532) I saw while browsing was catching all the exceptions related to not having the method/class. Not that I recommend it (proper code > brute forcing), but at least there won't be any crashes! – A--C Dec 19 '12 at 00:23
  • @A--C: If the code in question was using reflection, that technique is only needed if you are still supporting Android 1.x. – CommonsWare Dec 19 '12 at 00:34
  • Ah, I didn't realize. Thanks for that @CommonsWare :-) – A--C Dec 19 '12 at 00:43
  • [Build.VERSION_CODES.ICE_CREAM_SANDWICH](http://developer.android.com/reference/android/os/Build.VERSION_CODES.html#ICE_CREAM_SANDWICH) is added since API level 14. If you app is built against API level 14 or higher but allow it to run on older Android version, you may get runtime exception. Better to use `if (Build.VERSION.SDK_INT>=14) {...}` instead. – yorkw Dec 19 '12 at 01:55
  • 3
    @yorkw: not true. The Java compiler will just inline the literal value for that static constant. In other words, it will actually evaluate `Build.VERSION.SDK_INT>=14`. Of course you do still need to build against API level 14 or above in order to resolve compile-time errors. – MH. Dec 19 '12 at 03:20
  • Should be `Build.VERSION_CODES.ICE_CREAM_SANDWICH` instead of `Build.VERSION.ICE_CREAM_SANDWICH`. Could you correct the answer? – Sampo Aug 22 '18 at 12:01
0

You can check the current API level with Build.VERSION.SDK_INT. Use in if/case statement to select different code for different APIs. Also, if you have a minSdk that is set less, you might get errors or warnings about code specifically for later APIs. You can address these by annotating these later API classes with @TargetApi(APILEVEL)

iagreen
  • 31,470
  • 8
  • 76
  • 90