8

I have gone too long without asking this question. I have come across many methods in Android which are deprecated, but still work in newer versions of the API. So what is the risk of using a deprecated method as long as it works?

Here's a more specific question. I'm working with TimePickers and the getCurrentHour() method is deprecated in API 23 and replaced with getHour(). I obviously cannot use getHour() exclusively, since most devices are not yet on API 23, but is using getCurrentHour() "wrong" for the newest version of Android? Which of the following should I do?

  1. Continue using getCurrentHour() until years pass and API 23 becomes my new minSdkVersion?
  2. In code, explicitly check the API version and call either getCurrentHour() or getHour() based on the result?

Thank you for helping me learn.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
NSouth
  • 5,067
  • 7
  • 48
  • 83
  • `In code, explicitly check the API version and call either getCurrentHour() or getHour() based on the result?` this is the correct way – tyczj Sep 27 '15 at 13:12

1 Answers1

14

is using getCurrentHour() "wrong" for the newest version of Android?

If by "wrong" you mean that it will kill a puppy, no, it will not be wrong. And for API Level 23, it is unlikely to cause any sort of problem.

In general, "deprecated" means "we have something else that we would like you to use". What happens with deprecated stuff varies:

  • Sometimes, the deprecated stuff is still usable years later. AbsoluteLayout was deprecated in API Level 3, back in 2009. It still works as craptastically today as it did back then.

  • Sometimes, the deprecated stuff will have reduced functionality. Various methods on ActivityManager are deprecated and return a subset of what they used to, for security and privacy reasons.

  • Sometimes, the deprecated stuff will be removed outright (see: HttpClient in Android 6.0 (or, actually, don't see it, as it was removed)). This is fairly unusual.

Hence, a deprecation warning is a sign to pay attention and ponder what your long-term strategy will be for this particular bit of functionality.

Which of the following should I do?

For something as trivial as this method rename, you're probably safe with your first option (stick with the deprecated method until your minSdkVersion rises high enough).

For anything much bigger than that, though, I'd go with your second option. Sometimes, you will do that version check explicitly. Sometimes, you can use a library that will hide the version check for you. For example, pretty much anywhere in the support-v4 library that you see a ...Compat class (e.g., NotificationCompat, ContextCompat, ActivityCompat), the role of that class is to supply an API equivalent to the latest-and-greatest API level, but one that can be used on a wide range of devices, because it gracefully degrades on older devices by doing those version checks for you.

Note: no actual puppies were harmed in the creation of this answer

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • How does one go about finding out whether one can use a library which will do the job for you? I find myself facing the same issue as the OP with TimePicker and am just "doing it properly", i.e. checking if `android.os.Build.VERSION.SDK_INT>=23` before deciding which method to use. Studio still puts a crossed-out-line through the deprecated functions and gives me a warning though :-( Am I missing one or more tricks? – Yannick Oct 02 '15 at 09:50
  • @Yannick: "How does one go about finding out whether one can use a library which will do the job for you?" -- find libraries and read documentation for them. "Studio still puts a crossed-out-line through the deprecated functions and gives me a warning though" -- that's because they are deprecated. You can add a `@SuppressWarnings("deprecation")` annotation if you like (and if I remember the exact annotation value correctly...). – CommonsWare Oct 02 '15 at 10:46
  • But, what does happen with the web api calls being depricated? If they decide to remove it, app will at some moment stop working? For example, uploading stuff to Internet via some 3rd party lib that has deprecated v1 methods and now forcing to use v2 calls (but you still have to use v1 because of some old reasons). – Beemo Jan 23 '16 at 15:48
  • @Beemo: I do not understand what you are asking. I suggest that you ask a fresh Stack Overflow question, where you explain in detail what your concerns are. Be certain to explain precisely what "the web apis calls being depricated" means, along with who "they" is in "If they decide to remove it". – CommonsWare Jan 23 '16 at 16:01
  • I was talking that way because I didn't want to get into details of my problem. It's best if you could see my question on stack -> http://stackoverflow.com/questions/34913135/block-thread-while-uploading-picture-to-amazon-s3-on-android/34913442 By web APIs, I mean Amazon APIs that were working with those old v1 calls. – Beemo Jan 23 '16 at 16:12