Question

I want to be able to use the app on devices that run Android level 7, without this feature. If the device is API level 11 or newer, I want this feature to be available. Should I use @SuppressLint or @TargetApi(HONEYCOMB) and what's the difference?

enter image description here

Thanks!

Was it helpful?

Solution

Using TargetApi, you tell the compiler that you are sure that this class/method/whatever is never ever called unless the API requirements are met.

That's what you want to do. But that also means that your must make sure that this class is never called under this level of API. You can do that by using

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // Call that class
} else {
    // Call another class
}

OTHER TIPS

@SuppressLint 'NewApi' - tells Lint to ignore all warnings if the API that you are using was introduced after the minimum SDK version that you are using. This would suppress warnings about errors that might possibly occur if you later used an API that was beyond the level of HONEYCOMB (which is currently the minimum API level needed to run your project)

@TargetApi(HONEYCOMB) - tells Lint to treat this type as targeting a given API level ignoring any minimum API level that you may have mentioned. If you later referenced an API that is beyond the level of HONEYCOMB, Lint will again throw warnings about this.

Apart from this, you cannot run the app using later version APIs on a device which is capable of running only previous versions of Android which cannot interpret these APIs. While the app may load correctly, it will crash when it sees a call to this API as that version of Android does not understand what it is expected to do. You could run it without a problem on devices of level HONEYCOMB or later. If you wanted to run those apps on previous versions, you might want to consider using support libraries (if available).

You want to use TargetApi. The reason for that is that SuppressLint("NewApi") will swallow all lint errors, not only you using API level 11.

Let's say you later use API level 12. With TargetApi you will receive a new warning, so you can act on it. With SuppressLint("NewApi") you will not get anything. It's better to explicitly disable warnings, rather that disable them all forever.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top