以编程方式检查 Android 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26035310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:57:02  来源:igfitidea点击:

Checking Android version programmatically

android

提问by ssh

I need to check the Android version of my phone and if the version is less than 4.0.0 i should execute the following code otherwise it should not execute. How could I do it? I used this line to get the Android version.

我需要检查我手机的 Android 版本,如果版本低于 4.0.0,我应该执行以下代码,否则它不应该执行。我怎么能做到?我用这一行来获取 Android 版本。

String androidOS = Build.VERSION.RELEASE;

How could I check whether less than 4.0.0. Suggest me a solution.

我如何检查是否小于 4.0.0.0.0。建议我一个解决方案。

回答by Kushal Sharma

Something like this :

像这样的事情:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

}

and for more version check out here!

有关更多版本,请查看此处

回答by Bhanu Sharma

You can get any detail about phone like this -> and android.os.Build help you to get this

您可以像这样获得有关手机的任何详细信息 -> 和 android.os.Build 可以帮助您获得此信息

 System.out.println("button press on device name = "+android.os.Build.MODEL +" brand = "+android.os.Build.BRAND +" OS version = "+android.os.Build.VERSION.RELEASE + " SDK version = " +android.os.Build.VERSION.SDK_INT);

回答by Supriya

use

 int currentapiVersion = android.os.Build.VERSION.SDK_INT;


    if (currentapiVersion >= 14) {
        // Do something for 14 and above versions


    } else {

         // do something for phones running an SDK before 14

    }

回答by Pratik Dasa

Try below code:

试试下面的代码:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD){
    // Do your code for froyo and above versions
} else{
    // do your code for before froyo versions
}