Wednesday, 7 August 2013

How to get Wifi data counter in Android using TrafficStats class

How to get Wifi data counter in Android using TrafficStats class

I am using below code to get Traffic Data in Android using "TraficStats"
class
public class TrafficStatisticsDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/traffic_info"
android:padding="10dip"/>
The value it returns is for Mobile. Is there any way to get Wi-Fi value ?
Also i noticed There is lot of accuracy problem in discriminating.

No comments:

Post a Comment