Retrieving Android API version programmatically

前端 未结 11 1880
长情又很酷
长情又很酷 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:30

    try this:

     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
         // only for gingerbread and newer versions
     }
    
    0 讨论(0)
  • 2020-11-22 05:36

    android.os.Build.VERSION.SDK should give you the value of the API Level. You can easily find the mapping from api level to android version in the android documentation. I believe, 8 is for 2.2, 7 for 2.1, and so on.

    0 讨论(0)
  • 2020-11-22 05:41

    Like this:

    String versionRelease = BuildConfig.VERSION_NAME;
    
    versionRelease :- 2.1.17
    

    Please make sure your import package is correct ( import package your_application_package_name, otherwise it will not work properly).

    0 讨论(0)
  • 2020-11-22 05:42

    Got it. Its using the getApplicationInfo() method of the Context class.

    0 讨论(0)
  • 2020-11-22 05:48
    Build.VERSION.RELEASE;
    

    That will give you the actual numbers of your version; aka 2.3.3 or 2.2. The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a non standard OS (aka my android is running 2.3.5) and that will return a null when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter using standard Android version or not !

    To use it, you could just do this;

    String androidOS = Build.VERSION.RELEASE;
    
    0 讨论(0)
提交回复
热议问题