Maidul
Maidul

Reputation: 1287

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

Upvotes: 7

Views: 6962

Answers (2)

Philippe Girolami
Philippe Girolami

Reputation: 1876

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()

Upvotes: 0

Maidul
Maidul

Reputation: 1287

Please check that.

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);

Upvotes: 5

Related Questions