Springy
Springy

Reputation: 598

Understanding Android API Levels

Jelly Bean has come out and they have some pretty nice features. My question is that I want to target as many devices as possible, but at the same time have an app that can if possible use all the features in the highest API level.

So say for example I have this

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="16"/>

And I build my application on API 16. Is there a way to restrict certain parts of the code to say only run if you are running a device that is capable of running it? For example, If I want NFC, then only gingerbread devices and above can use the feature, else froyo down won't even see the feature.

Upvotes: 2

Views: 3216

Answers (2)

yorkw
yorkw

Reputation: 41126

Since ADT r16, we can use Lint scan and check project for all new API, check out my answer here for details:

NewApi: Finds API accesses to APIs that are not supported in all targeted API versions.

Alternatively, instead of conditional branch your code by feature (Stefano' answer), you can also conditional branch your code by API Level:

if (android.os.Build.VERSION.SDK_INT >= 9) {
  // do NFC stuff
}

Hope this helps.

Upvotes: 2

Stefano Ortisi
Stefano Ortisi

Reputation: 5336

Basically, you can use many of the new API introduced from Android 3.0 (for example, the Fragment engine), including the Android Support v4 as Referenced Library.

You can find some reference here:

http://developer.android.com/reference/android/support/v4/app/package-summary.html

and here:

http://developer.android.com/tools/extras/support-library.html

By the other hand, if you want use NFC feature for example, you can declare this use-feature:

 <uses-feature android:name="android.hardware.nfc" android:required="true" />

or, if you want provide code alternative for devices with no support for this feature, you can do something like that:

 if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
        //code for devices with NFC support
 } else {
      //code for others devices
 }

Upvotes: 6

Related Questions