Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:31:51+00:00 2026-06-12T11:31:51+00:00

My app is trying to count the number of bytes send and received over

  • 0

My app is trying to count the number of bytes send and received over WiFi/LAN and mobile data connections. To do that, I get the values of TrafficStats counters at one point in time and subtract that from its values the next time I check.

// get current values of counters
long currentMobileTxBytes = TrafficStats.getMobileTxBytes();
long currentMobileRxBytes = TrafficStats.getMobileRxBytes();
long totalTxBytes = TrafficStats.getTotalTxBytes();
long totalRxBytes = TrafficStats.getTotalRxBytes();

// to get mobile data count, subtract old from current
long currentMobileSent = currentMobileTxBytes - oldMobileTxBytes;
long currentMobileReceived = currentMobileRxBytes - oldMobileRxBytes;

// to get WiFi/LAN data count, subtract total from mobile
long currentNetworkSent = totalTxBytes - currentMobileTxBytes;
long currentNetworkReceived = totalRxBytes - currentMobileRxBytes;

I feel that the above algorithm is reasonable, however, I’m not sure how to check the accuracy of these counters. For example, when I tried uploading a 2.7MB file to Dropbox via WiFi, the currentMobileSent value I got was around 10MB. And even without surfing the web until the next check, I get non-zero values indicating that I did receive some bytes of data over the waiting period.

Is there a way for me to check how TrafficStats arrives at these numbers? I’m aware that besides my browser, there might be other applications running in the background that connect to the internet, but 2.7MB to 10MB just seems like a huge jump–I even “received” 90MB once without doing anything. Or is there something wrong with the way I’m computing the bytes sent and received?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:31:52+00:00Added an answer on June 12, 2026 at 11:31 am

    From TechRepublic:

    1. 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.

    2. 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"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical">
    
       <TextView
           android: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" />
    
       <TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="Transmit Bytes"
           android:textColor="#00ff00"
           android:textSize="14sp" />
    
       <TextView
           android:id="@+id/TX"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="0"
           android:textSize="14sp" />
    
       <TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:text="Receive Bytes"
           android:textColor="#ff0000"
           android:textSize="14sp" />
    
       <TextView
           android: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;
    
    public class Main extends Activity {
        private Handler mHandler = new Handler();
        private long mStartRX = 0;
        private long mStartTX = 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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mStartRX = TrafficStats.getTotalRxBytes();
        mStartTX = TrafficStats.getTotalTxBytes();
    
        if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
            AlertDialog.Builder alert = new AlertDialog.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

    private final Runnable mRunnable = new Runnable() {
        public void run() {
            TextView RX = (TextView) findViewById(R.id.RX);
            TextView TX = (TextView) findViewById(R.id.TX);
            long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
            RX.setText(Long.toString(rxBytes));
            long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            TX.setText(Long.toString(txBytes));
            mHandler.postDelayed(mRunnable, 1000);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a listing app that is core data backed.The first page
So I've been toiling over this app trying to figure out this strange behaviour
In my app I'm trying to implement a feature that helps the user to
In my Rails app I'm trying to get the MIME type of a file
I'm trying to count the number of unique users per day on my java
I am trying to count my array to set as the number of rows
I'm trying to port over some Core Data code over from a test Mac
In sp here I am trying to get a count of how many records
Hi I'm trying to use highcharts on my app...for that I'm following the highcharts
I have a core data based app I am working on, that uses an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.