有人知道我如何通过编程检查系统版本(例如1.0、2.2等)吗?


当前回答

检查android.os.Build.VERSION。

CODENAME:当前的开发代码名,如果这是一个发布版本,则为字符串“REL”。 INCREMENTAL:底层源代码控制用来表示此构建的内部值。 RELEASE:用户可见的版本字符串。

其他回答

使用这个类

import android.os.Build;

/**
 * Created by MOMANI on 2016/04/14.
 */
public class AndroidVersionUtil {
    public static int getApiVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    public static boolean isApiVersionGraterOrEqual(int thisVersion) {
        return android.os.Build.VERSION.SDK_INT >= thisVersion;
    }
}

构建。Version是获取该数据的地方。下面是如何格式化它的代码片段。

public String getAndroidVersion() {
    String release = Build.VERSION.RELEASE;
    int sdkVersion = Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
}

Android SDK: 19 (4.4.4)

注意Build.VERSION。SDK_INT不可靠,@Falcon165o提到过,最近我也遇到了这个问题。

因此,为了获得当前安装的Android的字符串数据(基于Android版本列表),我编写了如下代码:

Java

//Current Android version data
public static String currentVersion(){
    double release=Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\\d+[.]\\d+)(.*)","$1"));
    String codeName="Unsupported";//below Jelly Bean
    if(release >= 4.1 && release < 4.4) codeName = "Jelly Bean";
    else if(release < 5)   codeName="Kit Kat";
    else if(release < 6)   codeName="Lollipop";
    else if(release < 7)   codeName="Marshmallow";
    else if(release < 8)   codeName="Nougat";
    else if(release < 9)   codeName="Oreo";
    else if(release < 10)  codeName="Pie";
    else if(release >= 10) codeName="Android "+((int)release);//since API 29 no more candy code names
    return codeName+" v"+release+", API Level: "+Build.VERSION.SDK_INT;
}

科特林

fun currentVersion(): String {
    val release = java.lang.Double.parseDouble(java.lang.String(Build.VERSION.RELEASE).replaceAll("(\\d+[.]\\d+)(.*)", "$1"))
    var codeName = "Unsupported"//below Jelly Bean
    if (release >= 4.1 && release < 4.4)  codeName = "Jelly Bean"
    else if (release < 5)   codeName = "Kit Kat"
    else if (release < 6)   codeName = "Lollipop"
    else if (release < 7)   codeName = "Marshmallow"
    else if (release < 8)   codeName = "Nougat"
    else if (release < 9)   codeName = "Oreo"
    else if (release < 10)  codeName = "Pie"
    else if (release >= 10) codeName = "Android "+(release.toInt())//since API 29 no more candy code names
    return codeName + " v" + release + ", API Level: " + Build.VERSION.SDK_INT
}

它产生的输出示例:

棉花糖v6.0, API级别:23

根据Android文档:

release - "用户可见的版本字符串。例如,“1.0” 或者"3.4b5"或者"香蕉"

为了得到一个普通的Android版本(2.3,5.1,12.0等),我使用这个函数:

private fun getVersion(): Float {
    val release = Build.VERSION.RELEASE
    val parsedVersion = "\\d+(\\.\\d+)?".toRegex()
        .find(release)?.value
    if (parsedVersion.isNullOrBlank()) return 0f
    return try {
        parsedVersion.toFloat()
    } catch (e: Exception) {
        0f
    }
}

如果你的android设备上有bash,你可以使用这个bash函数:

function androidCodeName {
    androidRelease=$(getprop ro.build.version.release)
    androidCodeName=$(getprop ro.build.version.codename)

    # Time "androidRelease" x10 to test it as an integer
    case $androidRelease in
        [0-9].[0-9]|[0-9].[0-9].|[0-9].[0-9].[0-9])  androidRelease=$(echo $androidRelease | cut -d. -f1-2 | tr -d .);;
        [0-9].) androidRelease=$(echo $androidRelease | sed 's/\./0/');;
        [0-9]) androidRelease+="0";;
    esac

    [ -n "$androidRelease" ] && [ $androidCodeName = REL ] && {
    # Do not use "androidCodeName" when it equals to "REL" but infer it from "androidRelease"
        androidCodeName=""
        case $androidRelease in
        10) androidCodeName+=NoCodename;;
        11) androidCodeName+="Petit Four";;
        15) androidCodeName+=Cupcake;;
        20|21) androidCodeName+=Eclair;;
        22) androidCodeName+=FroYo;;
        23) androidCodeName+=Gingerbread;;
        30|31|32) androidCodeName+=Honeycomb;;
        40) androidCodeName+="Ice Cream Sandwich";;
        41|42|43) androidCodeName+="Jelly Bean";;
        44) androidCodeName+=KitKat;;
        50|51) androidCodeName+=Lollipop;;
        60) androidCodeName+=Marshmallow;;
        70|71) androidCodeName+=Nougat;;
        80|81) androidCodeName+=Oreo;;
        90) androidCodeName+=Pie;;
        100) androidCodeName+=ToBeReleased;;
        *) androidCodeName=unknown;;
        esac
    }
    echo $androidCodeName
}