检测Android N 版本代码
Detect Android N version code
是否可以检测用户是否 运行 Android N?
我有一个带有 Android N Developer Preview 的 Nexus 6。如果我尝试使用 Build.VERSION.SDK_INT
获取构建版本,它 returns 23 等于 Android Marshmallow.
引用 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));
}
我没有看过 Build.VERSION.RELEASE
,正如 zgc7009 的评论所建议的,尽管这也有可能。
此外,如果您是从遥远的未来阅读本文,其中 Android N 已经以最终形式发布,您应该能够使用 Build.VERSION.SDK_INT
和 Build.VERSION_CODES.N
。上述 hack 是由于 Google 处理这些开发人员预览的方式的特殊性。
我建议使用 Integer 值来检查 Android 版本而不是 String
public boolean isAndroidN() {
return Build.VERSION.SDK_INT == Build.VERSION_CODES.N;
}
请记住,在 manifests.xml 中必须将 compileSdkVersion 设置为 24 或更高:
compileSdkVersion 24
方法 1:(推荐)使用支持库 android.support.v4.os.BuildCompat.isAtLeastN
。
方法 2:使用此作为 "real" 版本代码:Build.VERSION.SDK_INT < 23 || Build.VERSION.PREVIEW_SDK_INT == 0 ? Build.VERSION.SDK_INT : Build.VERSION.SDK_INT + 1
.
我发现 Build.VERSION.RELEASE 和 Build.VERSION.CODENAME 的行为完全不同,具体取决于它是 Android OS 的完整生产版本还是开发人员预览版。我们采用了以下机制。如果你想考虑不止一种情况,你不能只依赖一个值。
这就是我发现的 Galaxy S7 运行 Nougat 生产版本和 Nexus 5X 运行 O DP1 的情况。
银河 S7 牛轧糖
Build.VERSION.BASE_OS:
Build.VERSION.CODENAME:相对
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:哦
Build.VERSION.INCREMENTAL:3793265
Build.VERSION.PREVIEW_SDK_INT: 1
Build.VERSION.RELEASE:哦
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");
}
}
是否可以检测用户是否 运行 Android N?
我有一个带有 Android N Developer Preview 的 Nexus 6。如果我尝试使用 Build.VERSION.SDK_INT
获取构建版本,它 returns 23 等于 Android Marshmallow.
引用 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));
}
我没有看过 Build.VERSION.RELEASE
,正如 zgc7009 的评论所建议的,尽管这也有可能。
此外,如果您是从遥远的未来阅读本文,其中 Android N 已经以最终形式发布,您应该能够使用 Build.VERSION.SDK_INT
和 Build.VERSION_CODES.N
。上述 hack 是由于 Google 处理这些开发人员预览的方式的特殊性。
我建议使用 Integer 值来检查 Android 版本而不是 String
public boolean isAndroidN() {
return Build.VERSION.SDK_INT == Build.VERSION_CODES.N;
}
请记住,在 manifests.xml 中必须将 compileSdkVersion 设置为 24 或更高:
compileSdkVersion 24
方法 1:(推荐)使用支持库 android.support.v4.os.BuildCompat.isAtLeastN
。
方法 2:使用此作为 "real" 版本代码:Build.VERSION.SDK_INT < 23 || Build.VERSION.PREVIEW_SDK_INT == 0 ? Build.VERSION.SDK_INT : Build.VERSION.SDK_INT + 1
.
我发现 Build.VERSION.RELEASE 和 Build.VERSION.CODENAME 的行为完全不同,具体取决于它是 Android OS 的完整生产版本还是开发人员预览版。我们采用了以下机制。如果你想考虑不止一种情况,你不能只依赖一个值。
这就是我发现的 Galaxy S7 运行 Nougat 生产版本和 Nexus 5X 运行 O DP1 的情况。
银河 S7 牛轧糖 Build.VERSION.BASE_OS: Build.VERSION.CODENAME:相对 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:哦 Build.VERSION.INCREMENTAL:3793265 Build.VERSION.PREVIEW_SDK_INT: 1 Build.VERSION.RELEASE:哦 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");
}
}