安卓如何计算网络使用数据包/流量

7
现在互联网接入非常昂贵。提供商收费很高。 我们不使用无限制的互联网计划。因此,我想开发一个应用程序来管理网络套餐/数据使用情况。
假设我已经激活了5GB的数据计划。所以在上网、上传/下载之前需要检查一下。
我知道有些数据提供商提供这种服务。我想通过我的应用程序在我的Android设备上捕获网络数据包。但是如何将它添加到我的代码中呢? 非常感谢您的帮助。

试试这个:http://developer.android.com/reference/android/net/TrafficStats.html - YuDroid
2个回答

5
请检查一下。
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);

当您使用这些方法获取字节和数据包时,它使用什么日期范围?过去的一周?一个月?永久? - CeejeeB
1
它获取自上次启动以来的信息。每次启动计数器都会重置为0。 - anddev84

0

非常感谢。您能否分享更多细节,例如链接一些教程... - Maidul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接