Detect Android N version code

混江龙づ霸主 提交于 2019-12-03 03:05:53

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.

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

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.

I found that the behaviour of Build.VERSION.RELEASE and Build.VERSION.CODENAME is quite different depending on whether it's a full production release of Android OS or a developer preview. We went with the following mechanism. You can't rely on just one value if you want to account for more than one scenario.

This is what I found was the case for a Galaxy S7 running a production release of Nougat and a Nexus 5X running O DP1.

Galaxy S7 Nougat Build.VERSION.BASE_OS: Build.VERSION.CODENAME: REL Build.VERSION.INCREMENTAL: G930FXXU1DQB3 Build.VERSION.PREVIEW_SDK_INT: 0 Build.VERSION.RELEASE: 7.0 Build.VERSION.SDK_INT: 24 Build.VERSION.SECURITY_PATCH: 2017-01-01

Nexus 5X O Build.VERSION.BASE_OS: Build.VERSION.CODENAME: O Build.VERSION.INCREMENTAL: 3793265 Build.VERSION.PREVIEW_SDK_INT: 1 Build.VERSION.RELEASE: O Build.VERSION.SDK_INT: 25 Build.VERSION.SECURITY_PATCH: 2017-03-05

// release builds of Android (i.e. not developer previews) have a CODENAME value of "REL"
    // check this. if it's REL then you can rely on value of SDK_INT (SDK_INT is inaccurate for DPs
    // since it has the same value as the previous version of Android)
    // if it's not REL, check its value. it will have a letter denoting the Android version (N for Nougat, O for... er... O and so on)

    boolean laterThanNougat = false;

    if(Build.VERSION.CODENAME.equals("REL")) {
        Log.i(TAG, "This is a release build");

        // since this is a release build, we can rely on value of SDK_INT
        if (android.os.Build.VERSION.SDK_INT > 25) {
            Log.i(TAG, "This is later than Nougat");
            laterThanNougat = true;
        } else {
            Log.i(TAG, "This is Nougat or before");
        }
    } else {
        Log.i(TAG, "This is NOT a release build");

        // since this is not a release build, we can't rely on value of SDK_INT. must check codename again
        if(Build.VERSION.CODENAME.compareTo("N") > 0) {
            Log.i(TAG, "This is later than Nougat");
            laterThanNougat = true;
        } else {
            Log.i(TAG, "This is Nougat or before");
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!