Handling different versions of Android

Most of the time throughout this book we haven't paid any attention to supporting older Android devices. The main reason being that all the up to date parts of the API we have been using work on such a high percentage of devices (in excess of 95%) it has not seemed worthwhile. Unless you intend carving out a niche, in apps for ancient Android relics this seems like a sensible approach. Regarding playing sounds however there have been some relatively recent modifications to the Android API.

Actually, this isn't immediately a big deal because devices newer than this can still use the old parts of the API. But it is good practice to specifically handle these differences in compatibility, because eventually, one day, the older parts might not work on newer versions of Android.

The main reason for discussing this here and now is that the slight differences in pre and post-Android Lollipop sound handling gives us a good excuse to see how we can deal with things like this in our code.

We will see how we can make our app compatible with the very latest devices and the pre Lollipop devices as well.

The class we will be using to make some noise is the SoundPool class. First, let's look at some simple code for detecting the current Android version.

Detecting the current Android version

We can determine the current version of Android using the static variables of the Build.Version class, SDK_INT and we can determine if it is newer than a specific version by comparing it to that versions Build.VERSION_CODES appropriate variable. If that explanation was a bit of a mouthful just look at how we determine if the current version is equal to or newer (greater) than Lollipop.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   
   // Lollipop or newer code goes here 
   
} else {

   // Code for devices older than lollipop here
   
}

Now let's see how to make a noise with Android devices newer, and then older, than Lollipop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.208.97