android 流量的統計

來源:互聯網
上載者:User

標籤:android 流量統計   procuid_stat   procnet   trafficstats   

1 android架構對流量的統計通過一個TrafficStats類可以直接擷取
    擷取總接受流量TrafficStats.getTotalRxBytes(),
    擷取總發送流量TrafficStats.getTotalTxBytes());
   擷取不包含WIFI的手機GPRS接收量TrafficStats.getMobileRxBytes());
   擷取不包含Wifi的手機GPRS發送量TrafficStats.getMobileTxBytes());

    統計某一個進程的總接收量TrafficStats.getUidRxBytes(Uid));
    統計某一個進程的總發送量TrafficStats.getUidTxBytes(Uid));

這些擷取的流量都是從一次開機到讀取時刻的統計量。
所以,統計某一個程式的流量統計的時候,一定要注意開關機,和本次開機後是第幾次啟動本程式。

2 android的TrafficStats類
前四個讀取的/proc/net/dev裡面的資料
後面的兩個介面對某一個進程的流量統計的是/proc/uid_stat/*** 介面裡面的節點 資料

package cn.sunzn.trafficmanger;
import android.app.Activity;
import android.net.TrafficStats;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       /** 擷取手機通過 2G/3G 接收的位元組流量總數 */
       TrafficStats.getMobileRxBytes();
       /** 擷取手機通過 2G/3G 接收的資料包總數 */
       TrafficStats.getMobileRxPackets();
       /** 擷取手機通過 2G/3G 發出的位元組流量總數 */
       TrafficStats.getMobileTxBytes();
       /** 擷取手機通過 2G/3G 發出的資料包總數 */
       TrafficStats.getMobileTxPackets();
       /** 擷取手機通過所有網路方式接收的位元組流量總數(包括 wifi) */
       TrafficStats.getTotalRxBytes();
       /** 擷取手機通過所有網路方式接收的資料包總數(包括 wifi) */
       TrafficStats.getTotalRxPackets();
       /** 擷取手機通過所有網路方式發送的位元組流量總數(包括 wifi) */
       TrafficStats.getTotalTxBytes();
       /** 擷取手機通過所有網路方式發送的資料包總數(包括 wifi) */
       TrafficStats.getTotalTxPackets();
       /** 擷取手機指定 UID 對應的應程式用通過所有網路方式接收的位元組流量總數(包括 wifi) */
       TrafficStats.getUidRxBytes(uid);
       /** 擷取手機指定 UID 對應的應用程式通過所有網路方式發送的位元組流量總數(包括 wifi) */
       TrafficStats.getUidTxBytes(uid);
   }
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.activity_main, menu);
       return true;
   }
}

Android OS下有幾個應用是集體的,包括(Android系統、設定儲存、設定、系統使用者介面、miui)

OS裡面的各個模組的流量統計都算到OS 1000的流量,如果某一模組出問題就不能夠揪出來,可以建立介面分別計算。

相關文章

聯繫我們

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