How to get total data usage of an android device programmatically?

3.1k Views Asked by At

In the settings menu on my android smartphone, I can see the total data usage of the current and past cycles. Can I somehow retrieve that information programmatically in my app?

Thanks in advance.

1

There are 1 best solutions below

3
On

You can use android.net.TrafficStats to get traffic details:

for example to get Total bytes received:

android.net.TrafficStats.getTotalRxBytes()

and if you want to get send and received info of your apps one by one you can get it like this:

first get all apps running info by this:

List<RunningAppProcessInfo>

then get UID of each app you want

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningApps = manager.getRunningAppProcesses();

for (RunningAppProcessInfo runningApp : runningApps) {
  // Get UID of the selected process
  int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;

  long received = TrafficStats.getUidRxBytes(uid);//received amount of each app
  long send   = TrafficStats.getUidTxBytes(uid);//sent amount of each app
}

let me know is this what you want