Stochastically
Stochastically

Reputation: 7846

Android notification builder getNotification() vs build()

According to the documentation that I've seen, the android notification builder was introduced in API 11, and:

That sounds straightforward enough, but how do I write code in Eclipse that'll call the right method depending on the API version?

Upvotes: 5

Views: 4023

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007296

If your app supports devices older than API Level 11, you should be using NotificationCompat.Builder, in which case you can just use build() all of the time.

Otherwise, you are welcome to just call getNotification(), until such time as you are willing to support only API Level 16 and higher. They simply renamed the method for greater consistency. If you look at the source code, getNotification() just calls build() on newer devices.

There's nothing wrong with using Raghav's approach, and that sort of technique will be required in other situations where there are API level differences.

Upvotes: 15

Raghav Sood
Raghav Sood

Reputation: 82563

You can check the API level at Runtime.

if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.JELLY_BEAN) {
  // call something for API Level 16+
} else if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.HONEYCOMB) {
  // call something for API Level 11+
}

Upvotes: 5

Related Questions