Android application traffic info


Android platform offers data about the installed applications. One of the most interesting one is the application data usage, i.e. how much traffic has the application generated.

Android platform offers this information through TrafficStats class. This class offers the data either as packet count or as transferred bytes. The following example shows how to get the data and show it.


To get the most out of this tutorial, you’ll need only basic knowledge of Android development. The example code uses as button to refresh the data, so if click handling is not familiar concept, then I suggest first to read my tutorial about that.



import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class TrafficStatsActivity extends Activity {

    private TextView mobileRxBytes;
    private TextView mobileRxPackets;

    private TextView totalRxBytes;
    private TextView totalRxPackets;

    protected void onCreate(Bundle savedInstanceState) {

        mobileRxBytes = (TextView) findViewById(;
        mobileRxPackets = (TextView) findViewById(;

        totalRxBytes = (TextView) findViewById(;
        totalRxPackets = (TextView) findViewById(;


     * Update traffic stats
    private void refreshTrafficStats() {

        mobileRxBytes.setText(TrafficStats.getMobileRxBytes() + "");
        mobileRxPackets.setText(TrafficStats.getMobileRxPackets() + "");

        totalRxBytes.setText(TrafficStats.getTotalRxBytes() + "");
        totalRxPackets.setText(TrafficStats.getTotalRxPackets() + "");

    public void onRefreshClick(View view) {

In the code above, refreshTrafficStats method reads the traffic information and updates the TextViews. Also if the user presses the Refresh button, the onRefreshClick is called and the traffic data is re-read and updated to the screen.

The example uses only few methods of the TrafficStats class. Checkout the JavaDoc for all available methods.


Initial traffic stats Updated traffic stats

Source code

Source code for this example project is available here

Further reading


Did you find this article helpful? Or are there parts that need some improving? Write your comment below and let me know what you think!

comments powered by Disqus