標籤:
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流程,假設一個模組不能揪出問題,您可以建立介面計算。
著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。
android 流量統計