android流量统计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android流量统计相关的知识,希望对你有一定的参考价值。

android.net.TrafficStats类中,提供了多种静态方法,可以直接调用获取,返回类型均为long型,如果返回等于-1代表 UNSUPPORTED 当前设备不支持统计。 
   static long  getMobileRxBytes()  //获取通过Mobile连接收到的字节总数,这里Android123提示大家不包含WiFi
   static long  getMobileRxPackets()  //获取Mobile连接收到的数据包总数
   static long  getMobileTxBytes()  //Mobile发送的总字节数
   static long  getMobileTxPackets()  //Mobile发送的总数据包数
   static long  getTotalRxBytes()  //获取总的接受字节数,包含Mobile和WiFi等
   static long  getTotalRxPackets()  //总的接受数据包数,包含Mobile和WiFi等
   static long  getTotalTxBytes()  //总的发送字节数,包含Mobile和WiFi等
   static long  getTotalTxPackets()  //发送的总数据包数,包含Mobile和WiFi等
static long  getUidRxBytes(int uid)  //获取某个网络UID的接受字节数
static long  getUidTxBytes(int uid) //获取某个网络UID的发送字节数
  最后Android开发网再次提醒开发者,TrafficStats类工作在Android 2.2 API Level为8的固件上。

 

 

 

Android有一个TrafficStats类可以直接获取
    总接受流量TrafficStats.getTotalRxBytes(),
    总发送流量TrafficStats.getTotalTxBytes());
   不包含WIFI的手机GPRS接收量TrafficStats.getMobileRxBytes());
   不包含Wifi的手机GPRS发送量TrafficStats.getMobileTxBytes());

    某一个进程的总接收量TrafficStats.getUidRxBytes(Uid));
    某一个进程的总发送量TrafficStats.getUidTxBytes(Uid));

这些都是从一次开机到一次关机的统计量。
所以,做某一个程序的流量统计的时候,一定要注意开关机,和本次开机后是第几次启动本程序。

2 android的上面的TrafficStats类
前四个读取的/proc/net/dev里面的数据


后面的某一个进程的流量统计的是/proc/uid_stat/*** 里面的 数据

        

现在google给的类库只能读取某app的本次开机发送流量和接受流量。
里面包含wifi和gprs。。

以上是关于android流量统计的主要内容,如果未能解决你的问题,请参考以下文章

Android流量统计TrafficStats类

Android应用流量统计——NetworkStatsManager使用

android流量统计

android统计每个APP的3g流量,如何?

Android项目实战_手机安全卫士流量统计

android—性能优化3—网络优化