How To Get The Correct Number Of Bytes Sent And Received In Trafficstats?
Solution 1:
From TechRepublic:
Create a new Android project in Eclipse. Remember to use the TrafficStats class you must target the API for Android 2.2 (Froyo) or higher.
In the
/res/layout
folder we will create a activity_main.xml resource. For this project, we are just using a series of text views in a vertically stacked linear layout.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:paddingBottom="20dip"android:text="Traffic Stats Demo"android:textSize="16sp"android:textStyle="bold" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="Transmit Bytes"android:textColor="#00ff00"android:textSize="14sp" /><TextViewandroid:id="@+id/TX"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="0"android:textSize="14sp" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="Receive Bytes"android:textColor="#ff0000"android:textSize="14sp" /><TextViewandroid:id="@+id/RX"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="0"android:textSize="14sp" /></LinearLayout>
With our layout in place we can move on to the /src folder. Create MainActivity.java by extending the Activity/AppCompatActivity class. Let’s also go ahead and declare three private class variables.
MainActivity.java
package com.authorwjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
publicclassMainextendsActivity {
privateHandlermHandler=newHandler();
privatelongmStartRX=0;
privatelongmStartTX=0;
}
We will use the on create override to initialize our private variables, as well as schedule a callback on the UI thread. Make a note of the check for the enum TrafficStats.UNSUPPORTED. While my experience with the TrafficStats class has been without a hitch, the official Google documentation states that some devices may not support this type of reporting and when that is the case the call returns the aforementioned value. For that reason it’s a good idea to write your code defensively, as I’ve demonstrated here.
MainActivity.java
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
AlertDialog.Builderalert=newAlertDialog.Builder(this);
alert.setTitle("Uh Oh!");
alert.setMessage("Your device does not support traffic stat monitoring.");
alert.show();
} else {
mHandler.postDelayed(mRunnable, 1000);
}
}
Last but not least we need to update our display and reschedule the runnable.
MainActivity.java
privatefinalRunnablemRunnable=newRunnable() {
publicvoidrun() {
TextViewRX= (TextView) findViewById(R.id.RX);
TextViewTX= (TextView) findViewById(R.id.TX);
longrxBytes= TrafficStats.getTotalRxBytes() - mStartRX;
RX.setText(Long.toString(rxBytes));
longtxBytes= TrafficStats.getTotalTxBytes() - mStartTX;
TX.setText(Long.toString(txBytes));
mHandler.postDelayed(mRunnable, 1000);
}
};
Post a Comment for "How To Get The Correct Number Of Bytes Sent And Received In Trafficstats?"