Android Question how to get android version?

Sagenut

Expert
Licensed User
Longtime User
Phone library
B4X:
Dim Version as Int
Version = Phone.SDKVersion
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim s As String = NativeMe.RunMethod("getAndroidVersion", Null)
Log("OSVersion=" & TAB & s)



#If JAVA
public String getAndroidVersion() {
    String release = android.os.Build.VERSION.RELEASE;
    int sdkVersion = android.os.Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
} 
#End If
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim s As String = NativeMe.RunMethod("getAndroidVersion", Null)
Log("OSVersion=" & TAB & s)



#If JAVA
public String getAndroidVersion() {
    String release = android.os.Build.VERSION.RELEASE;
    int sdkVersion = android.os.Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
}
#End If

thanks

that looked very promising
i tried it
go error on NativeME as undeclared so this is what i did

B4X:
#If JAVA
public String getAndroidVersion() {
    String release = android.os.Build.VERSION.RELEASE;
    int sdkVersion = android.os.Build.VERSION.SDK_INT;
    return "Android SDK: " + sdkVersion + " (" + release +")";
} 
#End If

Sub Get_AndroidVersion() As String
    Dim jo As JavaObject
    jo.InitializeContext

    'Dim s As String = NativeMe.RunMethod("getAndroidVersion", Null)
    
    Dim s As String = ""
    
    s = jo.RunMethod("getAndroidVersion", Null)

    Log("OSVersion=" & TAB & s)
    
    Return s

End Sub

still i get error

java.lang.RuntimeException: Method: getAndroidVersion not found in: ...

what am i doing wrong?
what am i missing?
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
so to summarize and check that i got you all guys right -
the sdk i get is the sdk of the device indicating the os version NOT the app's sdk
so i can rely on this as the android version of the phone itself
am i getting it right or wrong?

if i'm right - is there any function that translate the sdk to version so i won't be forced to keep on amending the function i will create?

thanks
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
so to summarize and check that i got you all guys right -
the sdk i get is the sdk of the device indicating the os version NOT the app's sdk
so i can rely on this as the android version of the phone itself
am i getting it right or wrong?

if i'm right - is there any function that translate the sdk to version so i won't be forced to keep on amending the function i will create?

thanks
please see this https://www.b4x.com/android/forum/threads/how-to-get-android-version.163649/post-1003726
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
In case it's useful, leave these functions that I use in my APK.


B4X:
public Sub GetVersionAndroid() As String
    If V_VersionAndroid = "" Then
        'Dim P As Phone
        V_VersionAndroid = GetSDKversion(False) ' P.SdkVersion
    End If
    Return V_VersionAndroid
End Sub

Sub GetSDKversion(SoloDevolverVersionComercial As Boolean) As String
    'https://developer.android.com/studio/releases/platforms
    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,"4.4W") '5.0p
    versions.Put(21,"5.0")
    versions.Put(22,"5.1")
    versions.Put(23,"6.0")
  '-----rest of versions...
  
    Dim p As Phone
    Dim Rt As String
    Rt = p.SdkVersion
  
    Try
        If SoloDevolverVersionComercial Then
            Rt = versions.Get(p.SdkVersion)
        Else
            Rt = Rt & ": " & versions.Get(p.SdkVersion) &  " " & p.Product
        End If
      
    Catch
        If SoloDevolverVersionComercial Then
            Rt = p.SdkVersion + "?"
        Else
            Rt = Rt & ": No en GetSDKversion"
        End If
        Log(LastException)
    End Try
    Return Rt
End Sub

Sub GetApiVesionSKD As String 'ignore
    Dim r As Reflector
    Dim Api As Int
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    Return Api
End Sub

GetApiVesionSKD is not used for me (may be have any problem!)
 
Last edited:
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
In case it's useful, leave these functions that I use in my APK.


B4X:
public Sub GetVersionAndroid() As String
    If V_VersionAndroid = "" Then
        'Dim P As Phone
        V_VersionAndroid = GetSDKversion(False) ' P.SdkVersion
    End If
    Return V_VersionAndroid
End Sub

Sub GetSDKversion(SoloDevolverVersionComercial As Boolean) As String
    'https://developer.android.com/studio/releases/platforms
    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,"4.4W") '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")
        versions.Put(29,"10.0")
       
        versions.Put(30,"11.0")
       
        versions.Put(31,"31.0")
        versions.Put(32,"32.0")
        versions.Put(33,"33.0")
        versions.Put(34,"34.0")
        versions.Put(35,"35.0")
        versions.Put(36,"36.0")
        versions.Put(37,"37.0")
        versions.Put(38,"38.0")
        versions.Put(39,"39.0")
   
    Dim p As Phone
    Dim Rt As String
    Rt = p.SdkVersion
   
    Try
        If SoloDevolverVersionComercial Then
            Rt = versions.Get(p.SdkVersion)
        Else
            Rt = Rt & ": " & versions.Get(p.SdkVersion) &  " " & p.Product
        End If
       
    Catch
        If SoloDevolverVersionComercial Then
            Rt = p.SdkVersion + "?"
        Else
            Rt = Rt & ": No en GetSDKversion"
        End If
        Log(LastException)
    End Try
    Return Rt
End Sub

Sub GetApiVesionSKD As String 'ignore
    Dim r As Reflector
    Dim Api As Int
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    Return Api
End Sub

GetApiVesionSKD is not used for me (may be have any problem!)
thanks for your reply and code sample
i think that the solution i got is much shorter and simpler (java based)
and most important it works and has no relation to sdk as i see in my handling so it is totally transparent and it's already being aplied in my project
anyways - thanks!
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
Thanks, I think I did this at the time, because I didn't know how to use inline java code in a module. Researching today (after several years), I have managed to find how to do it in this post that I consider very important and that is why I am attaching it here: How to execute inline java code anywhere in b4a:
(best solution: your solution!!!)

 
Upvote 0
Top