GetUidRxBytes () and getUidTxBytes () always return 0 in Android 4.3

I feel like crazy pills right now. The specific part of my application works very well for several days, and today it just stopped working, and I can’t understand why. This part of my code is used to display the total data from the moment of loading, which each specific application sent and received. Now the values ​​are always displayed as 0.

A few things that may or may not affect this:

1.) My Nexus 4 was recently upgraded to Android 4.3, but I doubt it is a problem because it worked right after my update.

2.) With the Android API 18 update, some methods from the traffic statistics API are now deprecated, but these are methods that I don’t even use, so this should not have any effect. http://developer.android.com/reference/android/net/TrafficStats.html

All help is appreciated.

PackageManager packageManager=this.getPackageManager(); List<ApplicationInfo> appList=packageManager.getInstalledApplications(0); for (ApplicationInfo appInfo : appList) { String appLabel = (String) packageManager.getApplicationLabel(appInfo); int uid = appInfo.uid; Log.d("data", String.valueOf(TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid))); 

Update [January 23, 2014]: Testing getUidRxBytes () and getUidTxBytes () on my Nexus 4 running Android 4.4.2 shows that the values ​​are no longer 0, but report statistics.

+7
android
source share
3 answers

The TrafficStats class retrieves network traffic information from the /proc/uid_stat/<uid> directory. It contains information about tcp, udp bytes and packets sent and received. If there are no files, the TrafficStats class will not be able to get network statistics. You can check if the files are present, if not, you're out of luck and need to look for another way.

If the files are present, you can try to read them yourself.

Also getUidTxBytes () and getUIDRxBytes () only report TCP traffic and allow UDP traffic. Therefore, if your application makes a lot of UDP traffic (for example, voip), you will not receive any information. There is already an error for this: https://code.google.com/p/android/issues/detail?id=32410

+3
source share

I reported this issue of the AOSP tracker: here

I also created an alternative solution to the problem that I inserted below:

 private Long getTotalBytesManual(int localUid){ File dir = new File("/proc/uid_stat/"); String[] children = dir.list(); if(!Arrays.asList(children).contains(String.valueOf(localUid))){ return 0L; } File uidFileDir = new File("/proc/uid_stat/"+String.valueOf(localUid)); File uidActualFileReceived = new File(uidFileDir,"tcp_rcv"); File uidActualFileSent = new File(uidFileDir,"tcp_snd"); String textReceived = "0"; String textSent = "0"; try { BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived)); BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent)); String receivedLine; String sentLine; if ((receivedLine = brReceived.readLine()) != null) { textReceived = receivedLine; } if ((sentLine = brSent.readLine()) != null) { textSent = sentLine; } } catch (IOException e) { } return Long.valueOf(textReceived).longValue() + Long.valueOf(textReceived).longValue(); } 
+4
source share

I talked about this in detail and explained some details, since the Android 4.3 TrafficStats API has changed the way it retrieves details from the device.

Prior to Android 4.3, UID traffic statistics were available for TCP and UDP and included APIs for bytes and packets, and were also sent and received. This data was extracted from the files / proc / uid _stat / [pid] / *.

In Android 4.3, developers decided to switch to a better and more secure API using the UID xt_qtaguid statistics, which is part of the Linux netfilter kernel module. This API (procfs) allows you to access based on the UID of the process, which is why when you try to access the TrafficStats API in Android => 4.3, you will get zero information for a non-native UID.

btw, the commit that caused the problem is the following: https://github.com/android/platform_frameworks_base/commit/92be93a94edafb5906e8bc48e6fee9dd07f5049e

* Improve the UID TrafficStats API. Transport layer statistics will become obsolete, leaving only generalized network layer statistics. Improve the documentation to be understandable about the layers where the measurements take place and their behavior from the moment of loading. Under the hood, move on to using UID statistics xt_qtaguid. Error: 6818637, 7013662 Change-Id: I9f26992e5fcdebd88c671e5765bd91229e7b0016 *

+2
source share

All Articles