我觉得我现在正在服用疯狂的药物。我的应用程序的一个特定部分几天来一直工作得很好,今天它停止工作了,我不明白为什么。我的代码的这一部分用于输出自启动以来每个特定应用程序发送和接收的总数据。现在,这些值始终显示为 0。
有几件事可能会或可能不会影响这一点:
1.) 我的 Nexus 4 今天刚刚更新到 Android 4.3,但我怀疑这是一个问题,因为更新后它运行得很好。
2.) 随着 Android API 18 更新,流量统计 API 中的一些方法现已弃用,但这些方法我什至没有使用,所以这应该没有效果。http://developer.android.com/reference/android/net/TrafficStats.html
非常感谢所有帮助。
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 日]:在运行 Android 4.4.2 的 Nexus 4 上测试 getUidRxBytes() 和 getUidTxBytes() 显示这些值不再是 0,而是报告正确的统计信息。
我已向 AOSP 问题跟踪器报告了该问题:here
我还为该问题创建了一个替代解决方案,粘贴在下面:
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)