getUidRxBytes()和getUidTxBytes()总是在安卓4.3返回0 [英] getUidRxBytes() and getUidTxBytes() always return 0 in Android 4.3

查看:984
本文介绍了getUidRxBytes()和getUidTxBytes()总是在安卓4.3返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我是在疯狂的药丸现在。我的应用程序的特定部分已经工作得很好了好几天了,今天刚刚停止工作,我想不出为什么。我的code这部分用于输出的总数据,因为开机即每一个具体的应用程序已经发送和接收。现在,值始终显示为0。

I feel like I am on crazy pills right now. A specific part of my application has been working just fine for several days, and today it just stopped working and I can not figure out why. This part of my code used to output the total data since boot that each specific app has sent and received. Now, the values always show up as 0.

这可能会或可能不会影响本有两件事情:

A couple things that may or may not be affecting this:

1。)我的Nexus 4刚刚更新到Android 4.3的今天,但我怀疑这是因为这个工作后,我更新了就好了正确的问题。

1.) My Nexus 4 was just updated to Android 4.3 today, but I doubt this is an issue because this worked just fine right after I updated.

2)随着Android的API 18日更新,从流量统计API的一些方法。如今,precated,但这些我都不使用方法,所以这应该没有任何效果。 http://developer.android.com/reference/android/net/TrafficStats.html

2.) With the Android API 18 update, some methods from the Traffic Stats API are now deprecated, but these are methods I am not even using, so this should have no effect. http://developer.android.com/reference/android/net/TrafficStats.html

所有的帮助是极大的AP preciated。

All help is greatly 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)));

更新[2014年1月23日]:我的Nexus 4运行Android 4.4.2测试getUidRxBytes()和getUidTxBytes()显示值不再为0,但报告正确统计数据。

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

推荐答案

在TrafficStats类获取有关网络流量从的/ proc / uid_stat /&LT信息; UID&GT; 目录。这包含了TCP,UDP字节,数据包发送和接收信息。如果文件不是present的TrafficStats类不能获取网络统计数据。您可以检查文件是否present,如果没有你的运气了,应该寻找其他的方式。

The TrafficStats class get the information about network traffic from the /proc/uid_stat/<uid> directory. This contains information about the tcp, udp bytes and packets sent and received. If the files are not present the TrafficStats class can't get the network stats. You can check if the files are present, If not you are out of luck and should look for other way.

如果该文件是present你可以尝试自己读它。

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

另外,getUidTxBytes()和getUIDRxBytes()只报告TCP流量而错过UDP流量。所以,如果您的应用程序做大量的UDP流量(如VoIP),那么你将得不到任何信息。 目前已经申请了这个错误: https://开头code.google.com / P /安卓/问题/详细信息?ID = 32410

Also the getUidTxBytes() and getUIDRxBytes() report only the TCP traffic and miss UDP traffic. So if your app is doing lots of UDP traffic (like voip) then you'll not get any info. There is already a bug filed for this : https://code.google.com/p/android/issues/detail?id=32410

这篇关于getUidRxBytes()和getUidTxBytes()总是在安卓4.3返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆