Android Question Mobile data questions

roy89

Member
Hello, I want to create an app that shows the mobile data being consumed in real time on my phone. I've already tried several options, but none of them work for me. If someone could give me an idea or share a YouTube tutorial or something from this group, it would help me understand how to get started, because honestly, I'm lost. Thank you.
I want to create something like this or similar.
 

Attachments

  • Screenshot_20251016_231840.jpg
    Screenshot_20251016_231840.jpg
    300.7 KB · Views: 15

zed

Well-Known Member
Licensed User
To display real-time mobile data consumption, you need to use native Android APIs via JavaObject objects.
Android provides the android.net.TrafficStats class which allows you to retrieve the bytes sent and received by the device.

getMobileRxBytes(): Bytes received via mobile data
getMobileTxBytes(): Bytes sent via mobile data

Full code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Private Timer1 As Timer
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

End Sub


Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
   
    Dim jo As JavaObject
    jo.InitializeStatic("android.net.TrafficStats")

    Dim rxBytes As Long = jo.RunMethod("getMobileRxBytes", Null)
    Dim txBytes As Long = jo.RunMethod("getMobileTxBytes", Null)

    Log("Data received : " & (rxBytes / 1024 / 1024) & " Mo")
    Log("Data sent : " & (txBytes / 1024 / 1024) & " Mo")
   
   
    Timer1.Initialize("Timer1", 5000) ' every 5 seconds
    Timer1.Enabled = True
   
End Sub

Sub Timer1_Tick
    Dim jo As JavaObject
    jo.InitializeStatic("android.net.TrafficStats")

    Dim rxBytes As Long = jo.RunMethod("getMobileRxBytes", Null)
    Dim txBytes As Long = jo.RunMethod("getMobileTxBytes", Null)

    Log("Reception: " & (rxBytes / 1024 / 1024) & " Mo")
    Log("Sending: " & (txBytes / 1024 / 1024) & " Mo")
End Sub
 
Upvote 0
Top