获取WiFi流量统计的android [英] Get wifi traffic stats android

查看:838
本文介绍了获取WiFi流量统计的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的深化发展一个应用程序,它允许检查在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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