¿Cómo puedo comprobar la versión del sistema de Android?


¿alguien sabe cómo puedo comprobar la versión del sistema (por ejemplo,1.0, 2.2, etc.¿programáticamente?

Author: slhck, 2010-06-22

12 answers

Comprobar android.os.Build.VERSION.

  • CODENAME: El nombre en clave de desarrollo actual, o la cadena "REL" si se trata de una compilación de lanzamiento.
  • INCREMENTAL: El valor interno utilizado por el control fuente subyacente para representar esta compilación.
  • RELEASE: La cadena de versión visible por el usuario.
 377
Author: Robby Pond,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-31 15:59:00

Ejemplo de cómo usarlo:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
}
 707
Author: ATom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-23 03:50:52

Construir.Version es el lugar al que ir para estos datos. Aquí hay un fragmento de código para cómo formatearlo.

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

Se parece a esto "Android SDK: 19 (4.4.4)"

 68
Author: jpotts18,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-13 23:03:06
Build.VERSION.RELEASE;

Que le dará los números reales de su versión; también conocido como 2.3.3 o 2.2. El problema con el uso de Build.VERSIÓN.SDK_INT es si tienes un teléfono rooteado o una rom personalizada, podrías tener un sistema operativo estándar none (también conocido como my android está ejecutando 2.3.5) y que devolverá un null al usar Build.VERSIÓN.SDK_INT so Build.VERSIÓN.¡La liberación funcionará sin importar qué!

 56
Author: Falcon165o,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-07 14:41:18

Usted puede encontrar la versión de Android mirando Build.VERSION.

La documentación recomienda comprobar Build.VERSION.SDK_INT contra los valores en Build.VERSION_CODES.

Esto está bien siempre y cuando te des cuenta de que Build.VERSION.SDK_INT solo se introdujo en el nivel de API 4, es decir, Android 1.6 (Donut). Por lo tanto, esto no le afectará, pero si desea que su aplicación se ejecute en Android 1.5 o anterior, entonces tendría que usar el obsoleto Build.VERSION.SDK en su lugar.

 42
Author: Dave Webb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-12-01 08:49:41

Para comprobar la versión del dispositivo que es mayor o igual a Marshmallow ,utilice este código.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){

    }

Para ckecking otros simplemente cambie los VERSION_CODES como,
K para kitkat,
L para loolipop N para turrón y así sucesivamente...

 32
Author: paulcab,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-27 08:36:33

No puedo comentar las respuestas, pero hay un gran error en la respuesta de Kaushik: SDK_INT no es lo mismo que la versión del sistema, pero en realidad se refiere al nivel de API.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    //this code will be executed on devices running ICS or later
}

El valor Build.VERSION_CODES.ICE_CREAM_SANDWICH es igual a 14. 14 es el nivel de API de Ice Cream Sandwich, mientras que la versión del sistema es 4.0. Por lo tanto, si escribe 4.0, su código se ejecutará en todos los dispositivos a partir de Donut, porque 4 es el nivel de API de Donut (Build.VERSION_CODES.DONUT es igual a 4).

if(Build.VERSION.SDK_INT >= 4.0){
    //this code will be executed on devices running on DONUT (NOT ICS) or later
}

Este ejemplo es una razón por la que usar 'número mágico' es un mala costumbre.

 27
Author: Michał Kisiel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-08 23:55:46

Por ejemplo, una característica solo funciona para api21 arriba lo siguiente corregimos errores en api21 abajo

    if(Build.VERSION.SDK_INT >= 21) {
    //only api 21 above
    }else{
   //only api 21 down
    }
 12
Author: HOSHYAR Ahmadpour,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-29 17:51:18

Tenga en cuenta que Construir.VERSIÓN.SDK_INT no es confiable, es mencionado por @Falcon165o y recientemente me encontré con eso también.

Así que para obtener los datos de cadena (basados en Android version list ) de Android actualmente instalado, hice un código como este:

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 OR above Oreo
    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";
    return codeName+" v"+release+", API Level: "+Build.VERSION.SDK_INT;
}

Kotlin

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 OR above Oreo
    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"
    return codeName + " v" + release + ", API Level: " + Build.VERSION.SDK_INT
}

Ejemplo de una salida que produce:

Marshmallow v6. 0, Nivel de API: 23

 10
Author: Nikita Kurtin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-05 07:23:39
if (Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.HONEYCOMB_MR2) {
//do anything you  like.
}
 7
Author: Kunkun Liu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-22 12:19:15

Utilice esta clase

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;
    }
}
 3
Author: Basheer AL-MOMANI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-14 17:21:06

Utilice este método:

 public static String getAndroidVersion() {
        String versionName = "";

        try {
             versionName = String.valueOf(Build.VERSION.RELEASE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }
 1
Author: Manoj Tarkar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-09 12:15:34