[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下載的總流量

   本質都是一樣的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM