Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does it say "deprecated" for a method when its the only one I can use for the selected API-level?

Tags:

android

sdk

In my Android manifest, it says this:

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

but when I write this code, the getNotification at the end gets me warning saying that the method is "deprecated":

Notification myNotification = new Notification.Builder(appContext)
.setContentTitle("SIC")
.setContentText(tickerText)
.setWhen(when)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.getNotification(); // <-- warning here

Now, the problem is that for API-level 10, which is the minimum I am developing for, getNotification is the only one there is to use. The newer method called "build()" is for API-level 16.

So why am I getting the deprecated warning even though its the only one I can and should use? One might think that the warning/docs should adapt to the minSdkLevel, not the highets one...

like image 417
Ted Avatar asked Dec 10 '12 19:12

Ted


People also ask

What does deprecated API mean?

Deprecation means that we've ended official support for the APIs, but they will continue to remain available to developers. This page highlights some of the deprecations in this release of Android. To see other deprecations, refer to the API diff report.

What is a deprecated method?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future.

What happens if we use deprecated methods in Java?

The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is. It's no longer relevant.

How do you call a deprecated method in Java?

Using the @Deprecated Annotation To use it, you simply precede the class, method, or member declaration with "@Deprecated." Using the @Deprecated annotation to deprecate a class, method, or field ensures that all compilers will issue warnings when code uses that program element.


1 Answers

So why am I getting the deprecated warning even though its the only one I can and should use?

Because your build target is API Level 16 or higher, where this method is deprecated.

One might think that the warning/docs should adapt to the minSdkLevel, not the highets one

In that case, one would be incorrect. Deprecation has nothing to do with minSdkVersion, any more than it would in standard Java outside of Android (where deprecation exists and minSdkVersion does not).

Moreover, you should be using the Android Support package's version of Notification.Builder (called NotificationCompat.Builder, since the native one does not exist in API Level 10, which your manifest indicates that you are trying to support. build() should exist on NotificationCompat.Builder and work on all your desired API levels.

like image 55
CommonsWare Avatar answered Oct 02 '22 13:10

CommonsWare