如何获取网络流量数据

F4b*_*o16 6 java linux android tethering

我正在做一个存储流量数据的应用程序,但我无法获得网络流量.

    // UIDs for Tethering ?
    appNames.put(5, "Tethering WiFi");
    appNames.put(7, "Tethering USB");
    appNames.put(10, "Tethering BT");
    appNames.put(11, "Tethering Unknown");


    appNames.put(-5, "!Tethering WiFi");
    appNames.put(-7, "!Tethering USB");
    appNames.put(-10, "!Tethering BT");
    appNames.put(-11, "!Tethering Unknown");

    for(ApplicationInfo app : cnt.getPackageManager().getInstalledApplications(0))
    {
        appNames.put(app.uid, app.packageName);
    }

    for(Integer uid: appNames.keySet())
    {
        apps.put(uid, new TrafficRecord(uid, appNames.get(uid)));
    }



TrafficRecord(int uid, String tag)
{
    tx = TrafficStats.getUidTxBytes(uid);
    rx = TrafficStats.getUidRxBytes(uid);
    this.tag = tag;
}
Run Code Online (Sandbox Code Playgroud)

但是,Tethering流量数据始终为空.我该怎么做?