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.

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Ginso
  • 459
  • 17
  • 33
  • 2
    Possible duplicate of [Android programmatically get data usage for specific app, for example: Data Usage used on "Facebook"](https://stackoverflow.com/questions/31922203/android-programmatically-get-data-usage-for-specific-app-for-example-data-usag) – Sreetam Das Jul 04 '17 at 09:16

1 Answers1

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

Meikiem
  • 1,876
  • 2
  • 11
  • 19
  • but this only gives me the usage from the apps that are currently running, doesn't it? – Ginso Jul 04 '17 at 08:42
  • the second part of answer, yes, but this android.net.TrafficStats.getTotalRxBytes() will give you your total usage – Meikiem Jul 04 '17 at 08: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 Jul 04 '17 at 11:55