B4A Library Device Information

Was a bit bored today and thought that I will try and do something like THIS. I found a Github Project that was able to provide me with some CPU information:

(Displayed in ListView1 of the attached project)
1. Maximum CPU Frequency
2. Minimum CPU Frequency
3. Current CPU Frequency

(Displayed in ListView2 of the attached project)
1. The number of cores
2. Combined utilization of all cores (%)
3 to x. The utilization of each individual core (%)

The two ListViews are updated every 500ms via a timer.

There are lots more functionality in the Github project but I was only interested in some of the CPU info. Have tested it on two devices and seems to be working....

Sample Code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aAndroidDeviceInformation
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim t As Timer
   

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Dim adi As AndroidDeviceInformation
    Dim freqlist As List
    Dim utillist As List
   

    Private lv1 As ListView
    Private lv2 As ListView
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    freqlist.Initialize
    utillist.Initialize
   
    lv1.Color = Colors.Blue
    lv1.SingleLineLayout.Label.TextSize = 15
   
    lv2.Color = Colors.Gray
    lv2.SingleLineLayout.Label.TextSize = 15
   
    t.Initialize("t", 500)
    adi.Initialize("cpu")

End Sub

Sub Activity_Resume
   
    t.Enabled = True

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    t.Enabled = False

End Sub

Sub t_tick
   
    adi.addFrequency
    adi.addCoreUtilization
   
End Sub

Sub cpu_found_frequency(freq As Object)
    lv1.Clear
    freqlist = freq
    lv1.AddSingleLine("MaxCpuFreq = " & freqlist.Get(0))
    lv1.AddSingleLine("MinCpuFreq = " & freqlist.Get(1))
    lv1.AddSingleLine("CurrentCpuFreq = " & freqlist.Get(2))
       
End Sub

Sub cpu_core_utilization(percentage As Object)
    lv2.Clear
    utillist = percentage
    lv2.AddSingleLine("No of Cores = " & utillist.Get(0))
    lv2.AddSingleLine("Utilization All Cores = " & utillist.Get(1))
    For i = 2 To utillist.Size - 1
        lv2.AddSingleLine("Utilization Core " & (i-2) & " = " & utillist.Get(i))
    Next
   
End Sub

1.png
 

Attachments

  • b4aLibFiles.zip
    95.2 KB · Views: 378
  • b4aAndroidDeviceInformation.zip
    8.2 KB · Views: 367
Last edited:
Top