zoukankan      html  css  js  c++  java
  • [Android Traffic] android 流量计算方法

    android流量简介

    • 流量统计文件:路径/proc/net/dev

    打开文件,其中 lo 为本地流量, rmnet0 为3g/2g流量, wlan0 为无线流量.

    • 在/sys/class/net/下 可以找到相关类别(如rmnet0)的目录.在其子目录statistics下游rx_bytes和tx_bytes记录收发流量.
    • 在/proc/uid_stat/{uid}/tcp_rcv记录该uid应用下载流量字节,/proc/uid_stat/{uid}/tcp_snd有该uid应用上传流量字节

    TrafficStats学习

    • TrafficStats google develop文档
    • TrafficStats 源文件 查看
    • 重要API:

    static long  getMobileRxBytes()  //获取通过Mobile连接收到的字节总数,不包含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的发送字节数[/mw_shl_code]
    备注:TrafficStats类在Android 2.2 API Level(8)之后出现。

      做GXB的时候查流量的方法是根据uid查询系统文件:

       /proc/uid_stat/uid/tcp_send        上传流量
       /proc/uid_stat/uid/tcp_rcv         下载流量

       做AVT的时候是通过uid调用系统的方法查询流量:

            //proc/uid_stat/10086
            long tx = TrafficStats.getUidTxBytes(uid);//发送的 上传的流量byte
            long rx = TrafficStats.getUidRxBytes(uid);//下载的流量 byte

            TrafficStats.getMobileTxBytes();//获取手机3g/2g网络上传的总流量
            TrafficStats.getMobileRxBytes();//手机2g/3g下载的总流量

            TrafficStats.getTotalTxBytes();//手机全部网络接口 包括wifi,3g、2g上传的总流量
            TrafficStats.getTotalRxBytes();//手机全部网络接口 包括wifi,3g、2g下载的总流量

       本质都是一样的。

  • 相关阅读:
    abstract修饰方法总结
    linux tar.gz zip 解压缩 压缩命令
    html5视频播放
    Response.Redirect 打开新窗体的两种方法
    Gmail POP3设置
    加壳学习笔记(二)-汇编基础
    C#-异常处理:tyr,catch,finally ---ShinePans
    cocos2d jsb 打包 Android APK
    编写你自己的单点登录(SSO)服务
    一分钟制作U盘版BT3
  • 原文地址:https://www.cnblogs.com/0616--ataozhijia/p/4312717.html
Copyright © 2011-2022 走看看