[Android Traffic] android 流量計算方法

來源:互聯網
上載者:User

標籤:

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

   本質都是一樣的。

[Android Traffic] android 流量計算方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.