Retrieving Android API version programmatically

前端 未结 11 1910
长情又很酷
长情又很酷 2020-11-22 04:54

Is there any way to get the API version that the phone is currently running?

11条回答
  •  遥遥无期
    2020-11-22 05:25

    As described in the Android documentation, the SDK level (integer) the phone is running is available in:

    android.os.Build.VERSION.SDK_INT

    The class corresponding to this int is in the android.os.Build.VERSION_CODES class.

    Code example:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
        // Do something for lollipop and above versions
    } else{
        // do something for phones running an SDK before lollipop
    }
    

    Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision).

    Corresponding android documentation is here and here

提交回复
热议问题