2012-09-05 38 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