5

I'm trying to get the traffic generated by my application. It includes a video streaming and I want to get network usage data.

I've used TrafficStats with Mobile and Total function. Now I want to set it to work using the application UID but I haven't managed to do so, as it always returns -1. I'm using Android 2.3.4 and here is the code:

int uid = getApplication().getApplicationInfo().uid;
uid = android.os.Process.myUid();
long txApp = TrafficStats.getUidTxBytes(uid);
long rxApp = TrafficStats.getUidRxBytes(uid);

With both uids I get an error (-1) on TrafficStats. Any help?

jlanza
  • 1,208
  • 3
  • 23
  • 43
  • Can you please share what was your mobile model ? I just want to see that how reliable TrafficStats.getUidTxBytes() is. Android docs also says that networkStats are not accessible in some phones. – Gem Sep 18 '14 at 13:11
  • I did all my developments with a Nexus S. I can try to look for more information but it's a project I finished 2 years ago. – jlanza Sep 22 '14 at 08:34

1 Answers1

4

It appears that your device does not support TrafficStats. The documentation states:

If the statistics are not supported by this device, UNSUPPORTED will be returned.

The code for TrafficStats.UNSUPPORTED is -1.

You can instead read the /sys/class/net/ log files. Try the code provided in this answer.

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • I forgot to put the answer. What so busy with the development. I did the way you say. Just as simple as reading a file ;) – jlanza Mar 01 '12 at 07:43
  • @paul Is that condition is so rare ? Cant we check this feature availability from manifest ? – Gem Sep 18 '14 at 13:15