2012-09-05 40 views
7

現在一天上網費用非常高昂。提供商使用地獄之類的費用。 我們不使用無限的互聯網計劃。這就是爲什麼我想開發一個可以計算(管理)網絡包/數據使用情況的工具。Android如何計算網絡使用數據包/數據

假設我已經激活了5GB的數據計劃。所以我需要在網上衝浪,上傳/下載東西之前檢查一下。

我知道一些數據提供者提供這些設施。我想通過我的應用程序捕獲我的android設備上的網絡數據包。但是,我怎樣才能將其寫入我自己的代碼中。 任何幫助將不勝感激。 謝謝提前

+0

試試這個:http://developer.android.com/reference/android/net/TrafficStats.html – YuDroid

回答

4

請檢查。

TextView infoView = (TextView)findViewById(R.id.traffic_info); 
String info = ""; 

info += "Mobile Interface:\n"; 
info += ("\tReceived: " + TrafficStats.getMobileRxBytes() + " bytes/" + TrafficStats.getMobileRxPackets() + " packets\n"); 
info += ("\tTransmitted: " + TrafficStats.getMobileTxBytes() + " bytes/" + TrafficStats.getMobileTxPackets() + " packets\n"); 

info += "All Network Interface:\n"; 
info += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes/" + TrafficStats.getTotalRxPackets() + " packets\n"); 
info += ("\tTransmitted: " + TrafficStats.getTotalTxBytes() + " bytes/" + TrafficStats.getTotalTxPackets() + " packets\n"); 

infoView.setText(info); 
+0

當你使用這些方法獲取字節和數據包時,它使用的是什麼日期範圍?過去的一週?月?永遠? – CeejeeB

+1

它從上次啓動時間開始獲取信息。每次啓動計數重置爲0。 – anddev84