Android流量統計TrafficStats類

來源:互聯網
上載者:User

標籤:

對於Android流量統計來說在2.2版中新加入了TrafficStats類可以輕鬆擷取,其實本身TrafficStats類也是讀取Linux提供的檔案對象系統類別型的文本進行解析。

android.net.TrafficStats類中,提供了多種靜態方法,可以直接調用擷取,傳回型別均為 long型,如果返回等於-1代表 UNSUPPORTED 當前裝置不支援統計。

static long  getMobileRxBytes()  //擷取通過Mobile串連收到的位元組總數,不包含WiFistatic 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的發送位元組數 

總接受流量TrafficStats.getTotalRxBytes(),
總發送流量TrafficStats.getTotalTxBytes());
不包含WIFI的手機GPRS接收量TrafficStats.getMobileRxBytes());
不包含Wifi的手機GPRS發送量TrafficStats.getMobileTxBytes());
某一個進程的總接收量TrafficStats.getUidRxBytes(Uid));
某一個進程的總發送量TrafficStats.getUidTxBytes(Uid));
這些都是從第一次啟動程式到最後一次啟動的統計量。並不是這篇文章裡所說的“從本次開機到本次關機的統計量”!
用法舉例,注意這裡得到的單位都是"KB"

public long getTotalRxBytes(){  //擷取總的接受位元組數,包含Mobile和WiFi等    return TrafficStats.getTotalRxBytes()==TrafficStats.UNSUPPORTED?0:(TrafficStats.getTotalRxBytes()/1024);  }  public long getTotalTxBytes(){  //總的發送位元組數,包含Mobile和WiFi等    return TrafficStats.getTotalTxBytes()==TrafficStats.UNSUPPORTED?0:(TrafficStats.getTotalTxBytes()/1024);  }  public long getMobileRxBytes(){  //擷取通過Mobile串連收到的位元組總數,不包含WiFi    return TrafficStats.getMobileRxBytes()==TrafficStats.UNSUPPORTED?0:(TrafficStats.getMobileRxBytes()/1024);  } 

 

參考連結:http://blog.csdn.net/qjbagu/article/details/7102730?locationNum=11

android_net_TrafficStats.cpp 源碼下載連結:http://download.csdn.net/detail/klcf0220/9654458

Android流量統計TrafficStats類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.