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: 68

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

roy89

Member
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

Thank you very much, my friend. It was very helpful to me. If it's not too much trouble, could you tell me how I can get the applications that are consuming data to show up in a list like the image I uploaded? I want the icons of the applications that are consuming data to be displayed. Honestly, this topic is very controversial and complicated for me. I have done some searches using artificial intelligence, but I haven't found the solution. Now all I can do is wait for you to respond. Perhaps you can help me further. I offer you a million thanks and I hope it doesn't bother you.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
  • Like
Reactions: zed
Upvote 0

zed

Well-Known Member
Licensed User
Here is some code that should have worked.
B4A:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
       
    ListAppsAndDataUsage
End Sub

Sub ListAppsAndDataUsage
    Dim pm As PackageManager
    Dim apps As List = pm.GetInstalledPackages
    For Each packageName As String In apps
        Try
            Dim uid As Int = GetAppUID(packageName)
            Dim usage As Map = GetDataUsageForUID(uid, packageName)
            Log("App: " & packageName)
            Log("  Received: " & usage.Get("rx") & " octets")
            Log("  Sent: " & usage.Get("tx") & " octets")
        Catch
            Log("Error for " & packageName)
        End Try
    Next
End Sub

Sub GetAppUID(packageName As String) As Int
    Dim joPM As JavaObject
    joPM.InitializeContext
    joPM = joPM.RunMethod("getPackageManager", Null)

    Dim joPI As JavaObject = joPM.RunMethod("getPackageInfo", Array(packageName, 0))
    Dim joAppInfo As JavaObject = joPI.GetField("applicationInfo")
    Return joAppInfo.GetField("uid")
End Sub

Sub GetDataUsageForUID(uid As Int, packageName As String) As Map
    Dim jo As JavaObject
    jo.InitializeContext

    Dim nsManager As JavaObject = jo.RunMethod("getSystemService", Array("netstats"))
    Dim startTime As Long = DateTime.Now - 24 * DateTime.TicksPerHour
    Dim endTime As Long = DateTime.Now

    Dim bucket As JavaObject = nsManager.RunMethod("querySummaryForDevice", Array(0, packageName, startTime, endTime))
    Dim rxBytes As Long = bucket.RunMethod("getRxBytes", Null)
    Dim txBytes As Long = bucket.RunMethod("getTxBytes", Null)

    Dim result As Map
    result.Initialize
    result.Put("rx", rxBytes)
    result.Put("tx", txBytes)
    Return result
End Sub


However, accessing network data by UID via NetworkStatsManager.queryDetailsForUid() is only possible if:
Your app is pre-installed as a system app or
You have special permissions (not accessible to regular apps)
You are on a rooted device or in advanced developer mode
In short: Android no longer allows regular apps to access other apps' network data via their UID, even if you can technically obtain this UID.

Result :
Error for zed.privatecamera
Error for com.android.providers.userdictionary
Error for com.google.android.apps.carrier.carrierwifi
Error for com.google.android.apps.setupwizard.searchselector
Error for com.android.emergency
Error for com.tinno.productInfo
Error for com.google.android.gms.location.history
Error for com.android.internal.systemui.navbar.gestural
Error for com.google.android.overlay.gmsconfig.personalsafety
Error for com.android.location.fused
Error for com.android.systemui
Error for com.google.android.ondevicepersonalization.services
Error for com.google.android.apps.youtube.music
Error for com.android.bluetoothmidiservice
Error for com.facebook.appmanager
Error for com.mediatek.networkstack.nongooverlay
Error for com.inmobi.weather
Error for com.android.traceur
Error for com.google.android.cellbroadcastreceiver
Error for anywheresoftware.b4a.b4abridge
Error for com.dreamgames.royalmatch
Error for com.vm3.global
Error for com.motorola.overlay.launcher3
Error for com.mediatek.sensorhub.ui
Error for android.auto_generated_rro_product__
Error for com.google.android.apps.adm
Error for com.android.bluetooth
Error for com.android.providers.contacts
Error for com.android.theme.icon.roundedrect
Error for com.motorola.installer
Error for com.maxmpz.audioplayer
Error for com.google.android.photopicker
Error for com.mediatek.gbaservice
Error for com.google.android.inputmethod.latin
Error for com.motorola.android.providers.chromehomepage
Error for com.dti.motorola
Error for com.google.android.apps.restore
Error for com.mediatek.MtkTelephonyServiceResOverlay
...
...
...
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
 
Upvote 0
Top