Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Android N version code

Is it possible to detect if an user is running Android N?

I have a Nexus 6 with the Android N Developer Preview. If I try to get the build version with Build.VERSION.SDK_INT, it returns 23 which is equal to Android Marshmallow.

like image 982
Tom Sabel Avatar asked Apr 01 '16 12:04

Tom Sabel


3 Answers

I would recommend using Integer value for checking Android version rather than String

public boolean isAndroidN() {
        return Build.VERSION.SDK_INT == Build.VERSION_CODES.N;
    }

Just remember it's necessary to have compileSdkVersion to 24 or higher in manifests.xml:

compileSdkVersion 24
like image 98
Maher Abuthraa Avatar answered Nov 12 '22 08:11

Maher Abuthraa


Quoting myself:

Following the approach that Google used for the M Developer Preview, you can check Build.VERSION.CODENAME instead:

public static boolean iCanHazN() {
  return("N".equals(Build.VERSION.CODENAME));
}

I haven't looked at Build.VERSION.RELEASE, as suggested by zgc7009's comment, though that too may be a possibility.

Also, if you are reading this from the far future, where Android N has shipped in final form, you should be able to use Build.VERSION.SDK_INT and Build.VERSION_CODES.N. The above hack is due to the idiosyncrasies of how Google handles these developer previews.

like image 11
CommonsWare Avatar answered Nov 12 '22 10:11

CommonsWare


Approach 1: (recommended) Use support library android.support.v4.os.BuildCompat.isAtLeastN.

Approach 2: Use this as the "real" version code: Build.VERSION.SDK_INT < 23 || Build.VERSION.PREVIEW_SDK_INT == 0 ? Build.VERSION.SDK_INT : Build.VERSION.SDK_INT + 1.

like image 2
Mygod Avatar answered Nov 12 '22 09:11

Mygod