Android如何通过wifi和移动设备了解每天的互联网总数据使用量 [英] Android how to know Internet total data usage per day through wifi and mobile

查看:150
本文介绍了Android如何通过wifi和移动设备了解每天的互联网总数据使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道每天的互联网总数据使用量?

How to know internet total data usage per day?

例如,在一天结束时,我使用了800mb,那么它应该像"2015年5月20日的Internet使用量为800mb"那样返回.

For example, at the end of the day I used 800mb then it should return like "internet usage of 800mb on 20th May 2015".

那我怎样才能检测到总的数据使用量呢?

So how can I detect total data usage ?

经过大量的搜索之后,我只能在发送和接收字节时发现数据使用情况,而在总使用情况中却找不到.

After much googling I could only find data usage in sending and receiving bytes but not in total usage.

并且还希望将使用情况分为wifi和移动数据.

And also want to split the usage into wifi and mobile data.

推荐答案

看看 getTotalRxBytes() getTotalTxBytes() getMobileRxBytes()

Take a look at the TrafficStats class. For this, you'll want to look specifically at getTotalRxBytes(), getTotalTxBytes(), getMobileRxBytes(), and getMobileTxBytes().

快速概述:

getTotalRxBytes = total downloaded bytes
getTotalTxBytes = total uploaded bytes

getMobileRxBytes = only mobile downloaded bytes
getMobileTxBytes = only mobile uploaded bytes

因此,为了仅获取与WiFi相关的流量的数量,您只需获取总数,然后减去移动设备,如下所示:

So, in order to get only the number for WiFi related traffic, you would only need to get the total, and subtract the mobile, as such:

getTotalRxBytes - getMobileRxBytes = only WiFi downloaded bytes
getTotalTxBytes - getMobileTxBytes = only WiFi uploaded bytes

使用字节数,我们可以切换到不同的单位,例如兆字节(MB):

With the number of bytes, we can switch to different units, such as megabytes (MB):

getTotalRxBytes / 1048576 = total downloaded megabytes

关于间隔(例如一天)的使用情况,由于这些方法仅提供总计(自引导以来),因此您需要跟踪起始数字,然后减去以获取一次使用期间的字节数.间隔.因此,在一天的开始(例如12:00:00 AM),您要跟踪总使用情况:

As for getting usage for an interval, for example a day, since these methods only provide the total (since boot), you will need to keep track of the beginning number and then subtract to get the number of bytes used during an interval. So, at the beginning of the day, such as 12:00:00AM, you keep track of the total usage:

startOfDay = getTotalRxBytes + getTotalTxBytes;

一天结束时(例如11:59:59 PM),您可以将两个数字相减得出当天的总使用量:

When the end of the day comes, such as 11:59:59PM, you can then subtract the two numbers and get the total usage for that day:

endOfDay    = getTotalRxBytes + getTotalTxBytes;
usageForDay = endOfDay - startOfDay;

总结:

  • 使用TrafficStats类提供的方法来获取互联网使用总数
  • 从总数中减去移动数据以仅获取WiFi使用情况
  • 使用转换率将字节转换为所需的任何单位
  • 在一天开始时存储使用量,然后在一天结束时减去使用量以获取当天的使用量

这篇关于Android如何通过wifi和移动设备了解每天的互联网总数据使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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