3

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

1 Answer 1

3

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

3
  • but this only gives me the usage from the apps that are currently running, doesn't it?
    – Ginso
    Commented Jul 4, 2017 at 8:42
  • the second part of answer, yes, but this android.net.TrafficStats.getTotalRxBytes() will give you your total usage
    – Meikiem
    Commented Jul 4, 2017 at 8:44
  • ok, but only since last device boot. But looking it up lead me to NetworkStatsManager and i think thats my solution. Will test it later
    – Ginso
    Commented Jul 4, 2017 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.