检测Android N版本代码

时间:2016-04-01 12:49:49

标签: android android-7.0-nougat

是否可以检测用户是否正在运行Android N?

我有一个带有Android N开发者预览版的Nexus 6。如果我尝试使用Build.VERSION.SDK_INT获取构建版本,则返回23,它等于Android Marshmallow。

4 个答案:

答案 0 :(得分:11)

引用myself

  

按照Google用于M Developer Preview的方法,   您可以检查Build.VERSION.CODENAME

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

我没有按照zgc7009评论的建议查看Build.VERSION.RELEASE,尽管这也许是可能的。

此外,如果您是在远期将来阅读此内容,而Android N已经以最终形式发布,那么您应该可以使用Build.VERSION.SDK_INTBuild.VERSION_CODES.N。上述黑客行为是由于谷歌处理这些开发者预览的特性所致。

答案 1 :(得分:2)

我建议使用Integer值来检查Android版本而不是String

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

请记住,在manifests.xml中将compileSdkVersion设置为24或更高是必要的:

compileSdkVersion 24

答案 2 :(得分:2)

方法1 :(推荐)使用支持库android.support.v4.os.BuildCompat.isAtLeastN

方法2:将此作为&#34;真实&#34;版本代码:Build.VERSION.SDK_INT < 23 || Build.VERSION.PREVIEW_SDK_INT == 0 ? Build.VERSION.SDK_INT : Build.VERSION.SDK_INT + 1

答案 3 :(得分:0)

我发现Build.VERSION.RELEASE和Build.VERSION.CODENAME的行为完全不同,这取决于它是Android OS的完整版本还是开发人员预览。我们采用以下机制。如果您想要考虑多个场景,则不能只依赖一个值。

这就是我发现运行Nougat的生产版本和运行O DP1的Nexus 5X的Galaxy S7的情况。

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");
        }
    }