Traffic from a specific application

I am trying to get traffic generated by my application. It includes streaming video, and I want to get network usage data.

I used TrafficStats with Mobile and Total. Now I want to configure it to work using the application UID, but I did not succeed, since it always returns -1. I am 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?

+4
source share
1 answer

It looks like your device does not support TrafficStats . The documentation states:

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

TrafficStats.UNSUPPORTED code is -1 .

Instead, you can read the log files /sys/class/net/ . Try the code provided in this answer .

+4
source

Source: https://habr.com/ru/post/1397280/


All Articles