以编程方式检索 Android API 版本 [英] Retrieving Android API version programmatically

查看:38
本文介绍了以编程方式检索 Android API 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以获取手机当前运行的API版本吗?

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

推荐答案

如 Android 文档中所述,手机运行的 SDK 级别(整数)可在:

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

android.os.Build.VERSION.SDK_INT

这个int对应的类在android.os.Build.VERSION_CODES 类.

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

代码示例:

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
}

编辑:此 SDK_INT 自 Donut (android 1.6/API4) 起可用,因此请确保您的应用程序在使用时不与 Cupcake (android 1.5/API3) 兼容,否则您的应用程序将崩溃(感谢程序员布鲁斯的精确性).

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).

对应的安卓文档是这里这里

这篇关于以编程方式检索 Android API 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆