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?
It appears that your device does not support
TrafficStats. The documentation states:The code for
TrafficStats.UNSUPPORTEDis-1.You can instead read the
/sys/class/net/log files. Try the code provided in this answer.