Android Code Snippet GetSDKVersion

subname: GetSDKVersion

Description: Returns the SDK-Version of the device running this code

B4X:
Sub GetSDKversion() As String
   Dim versions As Map
   versions.Initialize
   versions.Put(3,"1.5")
   versions.Put(4,"1.6")
   versions.Put(7,"2.1")
   versions.Put(8,"2.2")
   versions.Put(10,"2.3.3")
   versions.Put(11,"3.0")
   versions.Put(12,"3.1")
   versions.Put(13,"3.2")
   versions.Put(14,"4.0")
   versions.Put(15,"4.0.3")
   versions.Put(16,"4.1.2")
   versions.Put(17,"4.2.2")
   versions.Put(18,"4.3")
   versions.Put(19,"4.4.2")
   versions.Put(20,"5.0p")
   versions.Put(21,"5.0")
   versions.Put(22,"5.1")
   versions.Put(23,"6.0")
   versions.Put(24,"7.0")
   versions.Put(25,"7.1")
   versions.Put(26,"8.0")
   versions.Put(27,"8.1")
   versions.Put(28,"9.0")
   Dim p As Phone
   Return versions.Get(p.SdkVersion)
End Sub

Tags: SDK, SDKVersion

Example
B4X:
Log("SDKversion="&GetSDKversion) ' SDKversion=4.4.2 (on my Phone)
 
Last edited:

AscySoft

Active Member
Licensed User
Longtime User
This is good. But for completness please use this
B4X:
Sub GetSDKversion() As String
Try
    Dim versions As Map
    versions.Initialize
    versions.Put(1,"1.0 BASE")
    versions.Put(2,"1.1 BASE_1_1")
    versions.Put(3,"1.5 CUPCAKE")
    versions.Put(4,"1.6 DONUT")
    versions.Put (5,"2.0 ECLAIR")
    versions.Put (6,"2.0.1 ECLAIR_0_1")
    versions.Put(7,"2.1 ECLAIR_MR1")
    versions.Put(8,"2.2 FROYO")
    versions.Put (9,"2.3 GINGERBREAD")
    versions.Put(10,"2.3.3 GINGERBREAD_MR1")
    versions.Put(11,"3.0 HONEYCOMB")
    versions.Put(12,"3.1 HONEYCOMB_MR1")
    versions.Put(13,"3.2 HONEYCOMB_MR2")
    versions.Put(14,"4.0 ICE_CREAM_SANDWICH")
    versions.Put(15,"4.0.3 ICE_CREAM_SANDWICH_MR1")
    versions.Put(16,"4.1 JELLY_BEAN")
    versions.Put(17,"4.2 JELLY_BEAN_MR1")
    versions.Put(18,"4.3 JELLY_BEAN_MR2")
    versions.Put(19,"4.4 KITKAT")
    versions.Put(20,"4.4w KITKAT_WATCH")
    versions.Put(21,"5.0 LOLLIPOP") 
    versions.Put(22,"5.1 LOLLIPOP_MR1")'it is important that the last element should be the heigher sdk version available
    Dim p As Phone
    Dim api As Int = p.SdkVersion
    Dim vers As String = versions.Get(api)
    If api>versions.GetKeyAt (versions.Size -1) Then 'or If api>versions.Size Then 'version size should have all the elements in this case
          Log("A newer API is installed (api=" &api &"). Please update 'GetSDKVersion' function")
        Log ("Check this link: http://developer.android.com/guide/topics/manifest/uses-sdk-element.html")
        Return api
    End If
    Return vers
Catch
    Log(LastException)
    Return "error"
End Try
End Sub
Hope it helps
 
Last edited:

JOTHA

Well-Known Member
Licensed User
Longtime User
When I use your code, I get an error:

Does anyone have an idea?
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Hello Erel,
thank you for your answer.

Here is the Sub:
B4X:
Sub GetSDKversion() As String

Try
    
    Dim versions As Map
    versions.Initialize
    
'-- listet nur die Versionsnummern auf --   
    versions.Put(3, "1.5")
    versions.Put(4, "1.6")
    versions.Put(7, "2.1")
    versions.Put(8, "2.2")
    versions.Put(10, "2.3.3")
    versions.Put(11, "3.0")
    versions.Put(12, "3.1")
    versions.Put(13, "3.2")
    versions.Put(14, "4.0")
    versions.Put(15, "4.0.3")
    versions.Put(16, "4.0.4")
    versions.Put(17, "4.1.2")
    versions.Put(18, "4.2.2")
    versions.Put(19, "4.3")
    versions.Put(20, "4.4.2")
    versions.Put(21, "5.0p")
    versions.Put(22, "5.0")
    versions.Put(23, "5.1")
    versions.Put(24, "6.0")
    versions.Put(27, "7.0")
    
'-- listet die Versionsnummern mit Versionsnamen auf --   
'    versions.Put(1, "1.0 BASE")
'    versions.Put(2, "1.1 BASE_1_1")
'    versions.Put(3, "1.5 CUPCAKE")
'    versions.Put(4, "1.6 DONUT")
'    versions.Put(5, "2.0 ECLAIR")
'    versions.Put(6, "2.0.1 ECLAIR_0_1")
'    versions.Put(7, "2.1 ECLAIR_MR1")
'    versions.Put(8, "2.2 FROYO")
'    versions.Put (9, "2.3 GINGERBREAD")
'    versions.Put(10, "2.3.3 GINGERBREAD_MR1")
'    versions.Put(11, "3.0 HONEYCOMB")
'    versions.Put(12, "3.1 HONEYCOMB_MR1")
'    versions.Put(13, "3.2 HONEYCOMB_MR2")
'    versions.Put(14, "4.0 ICE_CREAM_SANDWICH")
'    versions.Put(15, "4.0.3 ICE_CREAM_SANDWICH_MR1")
'    versions.Put(16, "4.1 JELLY_BEAN")
'    versions.Put(17, "4.2 JELLY_BEAN_MR1")
'    versions.Put(18, "4.3 JELLY_BEAN_MR2")
'    versions.Put(19, "4.4 KITKAT")
'    versions.Put(20, "4.4w KITKAT_WATCH")
'    versions.Put(21, "5.0 LOLLIPOP")
'    versions.Put(22, "5.1 LOLLIPOP_MR1")  '-- it is important that the last element should be the heigher sdk version available --
    
    Dim p As Phone
    Dim api As Int = p.SdkVersion
    Dim vers As String = versions.Get(api)
    If api>versions.GetKeyAt (versions.Size -1) Then  '-- or If api>versions.Size Then 'version size should have all the elements in this case --
        Log("Eine neuere API ist installiert (api=" &api &"). Please update 'GetSDKVersion' function")
        Log ("Check diesen link: http://developer.android.com/guide/topics/manifest/uses-sdk-element.html")
        
        Msgbox("Android-Version: "&GetSDKversion&"", "")

        Return api
    End If
    
    Msgbox("Android-Version: "&GetSDKversion&"", "")
    
    Return vers
    
Catch
    Log(LastException)
    Msgbox("Android-Version: "&LastException&"", "")
    Return "error"
End Try

'    Log("-- 30488 -- SDKversion: "&GetSDKversion&" --")
'
'    Msgbox("Android-Version: "&GetSDKversion&"", "")
'    Return versions.Get(p.SdkVersion)
    
End Sub

I call the Sub by clicking a button like this:
GetSDKversion
 

JOTHA

Well-Known Member
Licensed User
Longtime User
I made an example attached as ZIP-File.
It doesn't work.

Here is the Log-File:

Whats wrong with it?
 

Attachments

  • GetVersion_Example.zip
    8 KB · Views: 362

DonManfred

Expert
Licensed User
Longtime User

JOTHA

Well-Known Member
Licensed User
Longtime User
Thank you very much, DonManfred!
Now it works.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…