歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android流量監測的實現原理

Android流量監測的實現原理

日期:2017/3/1 10:19:00   编辑:Linux編程

Android 2.2版本以前的,系統的流量信息都存放在proc/net/dev(或者proc/self/net/dev)文件下,讀取文件然後對其進行解析就行了。

讀取某一個應用的流量,則讀取proc/uid_stat/uid/tcp_rcv文件進行解析(據說模擬器下是沒有這個目錄文件的)

對於Android流量統計來說在2.2版中加入了TrafficStats類可以輕松獲取,其實本身TrafficStats類也是讀取Linux提供的文件對象系統類型的文本進行解析。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的發送字節數

Copyright © Linux教程網 All Rights Reserved