问题描述
我的深化发展一个应用程序,它允许检查在Android WiFi和移动流量统计。这就是我如何得到数据:
I am developping an app which allows to check wifi and mobile traffic stats on android. That's how I get the stats :
long mobileStats = TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes();
long wifiStats = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes() - mobileStats;
不幸的是, wifiStats
在这里似乎比只因为即使当我在我的智能手机的WiFi关闭,这让我吨的数据无线多。
我认为, getTotalRxBytes()
和 getTotalTxBytes()
指望传输的字节数和所有网络接口接收。
Unfortunately, wifiStats
here seems to be more than wifi only because even when i disable wifi on my smartphone, it gets me tons of data.I think that getTotalRxBytes()
and getTotalTxBytes()
are counting bytes transmitted and received on all Network Interfaces.
我在网上搜索如何获得流量统计仅在WiFi,但我不能找到一个办法了很多。
I searched a lot on the web how to get traffic stats only on wifi but i cannot find a way.
我很乐意接受任何帮助。
I would gladly accept any help.
推荐答案
我有同样的问题在几年前,并通过读取系统解决了这个文件直接。
I had the same problem a few years back and solved this by reading the system files directly.
private final String RX_FILE = "/sys/class/net/wlan0/statistics/rx_bytes";
private final String TX_FILE = "/sys/class/net/wlan0/statistics/tx_bytes";
private long readFile(String fileName){
File file = new File(fileName);
BufferedReader br = null;
long bytes = 0;
try{
br = new BufferedReader(new FileReader(file));
String line = "";
line = br.readLine();
bytes = Long.parseLong(line);
} catch (Exception e){
e.printStackTrace();
return 0;
} finally{
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bytes;
}
希望它帮助!
这篇关于获取WiFi流量统计的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!