These statistics may not be available on all platforms. If the statistics are not supported by this device, UNSUPPORTED will be returned.
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:text="@string/hello"
/>
<Button
android:id="@+id/loadtrafficstats"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load TrafficStats"
/>
<TextView
android:id="@+id/MobileRxBytes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/MobileRxPackets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/MobileTxBytes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/MobileTxPackets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TotalRxBytes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TotalRxPackets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TotalTxBytes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TotalTxPackets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidTrafficStats.java
package com.exercise.AndroidTrafficStats;
import android.app.Activity;
import android.net.TrafficStats;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidTrafficStats extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textMobileRxBytes = (TextView)findViewById(R.id.MobileRxBytes);
final TextView textMobileRxPackets = (TextView)findViewById(R.id.MobileRxPackets);
final TextView textMobileTxBytes = (TextView)findViewById(R.id.MobileTxBytes);
final TextView textMobileTxPackets = (TextView)findViewById(R.id.MobileTxPackets);
final TextView textTotalRxBytes = (TextView)findViewById(R.id.TotalRxBytes);
final TextView textTotalRxPackets = (TextView)findViewById(R.id.TotalRxPackets);
final TextView textTotalTxBytes = (TextView)findViewById(R.id.TotalTxBytes);
final TextView textTotalTxPackets = (TextView)findViewById(R.id.TotalTxPackets);
Button buttonLoadTrafficStats = (Button)findViewById(R.id.loadtrafficstats);
buttonLoadTrafficStats.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
if(TrafficStats.getMobileRxBytes() == TrafficStats.UNSUPPORTED)
{
textMobileRxBytes.setText("MobileRxBytes: " + "UNSUPPORTED!");
}
else{
textMobileRxBytes.setText("MobileRxBytes: "
+ String.valueOf(TrafficStats.getMobileRxBytes()));
}
if(TrafficStats.getMobileRxPackets() == TrafficStats.UNSUPPORTED)
{
textMobileRxPackets.setText("MobileRxPackets: " + "UNSUPPORTED!");
}
else{
textMobileRxPackets.setText("MobileRxPackets: "
+ String.valueOf(TrafficStats.getMobileRxPackets()));
}
if(TrafficStats.getMobileTxBytes() == TrafficStats.UNSUPPORTED)
{
textMobileTxBytes.setText("MobileTxBytes: " + "UNSUPPORTED!");
}
else{
textMobileTxBytes.setText("MobileTxBytes: "
+ String.valueOf(TrafficStats.getMobileTxBytes()));
}
if(TrafficStats.getMobileTxPackets() == TrafficStats.UNSUPPORTED)
{
textMobileTxPackets.setText("MobileTxPackets: " + "UNSUPPORTED!");
}
else{
textMobileTxPackets.setText("MobileTxPackets: "
+ String.valueOf(TrafficStats.getMobileTxPackets()));
}
if(TrafficStats.getTotalRxBytes() == TrafficStats.UNSUPPORTED)
{
textTotalRxBytes.setText("TotalRxBytes: " + "UNSUPPORTED!");
}
else{
textTotalRxBytes.setText("TotalRxBytes: "
+ String.valueOf(TrafficStats.getTotalRxBytes()));
}
if(TrafficStats.getTotalRxPackets() == TrafficStats.UNSUPPORTED)
{
textTotalRxPackets.setText("TotalRxPackets: " + "UNSUPPORTED!");
}
else{
textTotalRxPackets.setText("TotalRxPackets: "
+ String.valueOf(TrafficStats.getTotalRxPackets()));
}
if(TrafficStats.getTotalTxBytes() == TrafficStats.UNSUPPORTED)
{
textTotalTxBytes.setText("TotalTxBytes: " + "UNSUPPORTED!");
}
else{
textTotalTxBytes.setText("TotalTxBytes: "
+ String.valueOf(TrafficStats.getTotalTxBytes()));
}
if(TrafficStats.getTotalTxPackets() == TrafficStats.UNSUPPORTED)
{
textTotalTxPackets.setText("TotalTxPackets: " + "UNSUPPORTED!");
}
else{
textTotalTxPackets.setText("TotalTxPackets: "
+ String.valueOf(TrafficStats.getTotalTxPackets()));
}
}});
}
}
Download the files.