Android的TrafficStats getTotalRxBytes()返回始终为零 [英] Android TrafficStats getTotalRxBytes() returns always zero

查看:2418
本文介绍了Android的TrafficStats getTotalRxBytes()返回始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着来衡量多少字节我的应用程序已经收到。 林做这种方式:

Im trying to measure how much bytes my app has received. Im doing it this way:

long receivedBytesBefore = TrafficStats.getTotalRxBytes();
...
doSomething();
...
long receivedBytesAfter = TrafficStats.getTotalRxBytes();
long receivedBytes = receivedBytesAfter - receivedBytesBefore;

我的问题是getTotalRxBytes()总是返回0。所以,我的结果是0,无论我做什么。 我发现,该方法只是阅读一些TEXTFILES像

My problem is that getTotalRxBytes() always returns 0. So my result is 0 no matter what I do. I have found out that the method is just reading some textfiles like

/sys/class/net/rmnet0/statistics/rx_bytes

所以,我看着这些文件,它们都仅包含0。

So I looked into these files and they all contain just "0".

难道我错过了什么或做我必须以某种方式激活此功能? 是否有另一种方法来衡量多少字节我的应用程序已经收到?

Do I miss something or do I have to activate this function somehow? Is there another way to measure how much bytes my app has received?

我的Andr​​oid设备上运行的三星Galaxy王牌S5830的Andr​​oid 2.3.3

My Android device is a Samsung Galaxy Ace S5830 running Android 2.3.3

推荐答案

我可以确认这是发生在我身上也。

I can verify this was happening to me as well.

从行为我所观察到的,看来getTotalRxBytes当无线连接才有效。但是一个需要注意的是,如果你想获得接收例如文件的字节的准确数量,有接缝要发送一些额外的字节。

From behavior I've observed, it appears that getTotalRxBytes only works when wifi is connected. But something to be aware of is that if you are trying to get an accurate number of bytes received for a file for example, there seams to be some extra bytes sent.

所以,如果你不需要它是超级准确。您可以使用getMobileRxBytes(),以便当WiFi是不活动的,getTotalRxBytes(),以便当无线网络处于活动状态。

So if you don't need it to be super accurate. You can use getMobileRxBytes() for when wifi is not active and getTotalRxBytes() for when wifi is active.

下面是一个简单的例子。

Here is a simple example.

例如:

    ConnectivityManager connManager;
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    long initialBytes = 0;
    long finalBytes = 0;
    long byteDifference = 0;
    boolean onWifi= false;

    if (mWifi.isConnected())
    {
     //wifi connected
     initialBytes = TrafficStats.getTotalRxBytes();
     onWifi = true;
    }
    else if (mMobile.isConnected()) 
    {
    //if 3g/4g connected
     initialBytes = TrafficStats.getMobileRxBytes();
     onWifi = true;
    }
    else
    {
     //Something funny going on
     Log.e("Error", "Something funny going on");
     return;
    }


// Process whatever you want to process


    if(onWifi)
    {
      finalBytes = TrafficStats.getTotalRxBytes();
    }
    else
    {
      finalBytes = TrafficStats.getMobileRxBytes();
    }

    byteDifference  = finalBytes - initialBytes;

东西沿着这些路线。希望这有助于。

Something along these lines. Hopefully this helps.

这篇关于Android的TrafficStats getTotalRxBytes()返回始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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