Android How to calculate network usage packet/data

Now a days Internet access is very costly.The provider used charges like hell. We do not use unlimited plan for internet.That's why I would like to develop an that can facilities to calculate(manage) network package/data usage.

Suppose I have activated 5GB data plan.So I need to check before surfing net,upload/download things.

I know some Data provider provides these facility .I want to capture network data packets on my android device by my app. But How can I do that into my own code. Any help will be appreciated. Thank you advance


请检查。

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

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

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

infoView.setText(info);

Use TrafficStats class starting in APILevel 8. You get overall usage per radio access technology and per app.

See http://developer.android.com/reference/android/net/TrafficStats.html It's all static methods, for example call TrafficStats.getMobileRxBytes()

链接地址: http://www.djcxy.com/p/63306.html

上一篇: 我的单元测试是否关心太多

下一篇: Android如何计算网络使用数据包/数据