title: Show Upload and Download Speed in android
body: <p>First off, this question have been asked before also (i did a 1 hour research) but none of them seem to answer the question. so here it goes-
  i want to get <strong>current Upload and Download speed</strong> using TrafficStats and show it in my textView where i am able to show Up and Down stats in the below code (i got this code from a question)</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private Handler mHandler = new Handler();
      private long mStartRX = 0;
      private long mStartTX = 0;
      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 rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
              RX.setText(Long.toString(rxBytes) + " bytes");
              if (rxBytes &gt;= 1024) {
                  // KB or more
                  long rxKb = rxBytes / 1024;
                  RX.setText(Long.toString(rxKb) + " KBs");
                  if (rxKb &gt;= 1024) {
                      // MB or more
                      long rxMB = rxKb / 1024;
                      RX.setText(Long.toString(rxMB) + " MBs");
                      if (rxMB &gt;= 1024) {
                          // GB or more
                          long rxGB = rxMB / 1024;
                          RX.setText(Long.toString(rxGB));
                      }// rxMB&gt;1024
                  }// rxKb &gt; 1024
              }// rxBytes&gt;=1024
  
              // long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
              // TX.setText(Long.toString(txBytes));
              long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
              TX.setText(Long.toString(txBytes) + " bytes");
              if (txBytes &gt;= 1024) {
                  // KB or more
                  long txKb = txBytes / 1024;
                  TX.setText(Long.toString(txKb) + " KBs");
                  if (txKb &gt;= 1024) {
                      // MB or more
                      long txMB = txKb / 1024;
                      TX.setText(Long.toString(txMB) + " MBs");
                      if (txMB &gt;= 1024) {
                          // GB or more
                          long txGB = txMB / 1024;
                          TX.setText(Long.toString(txGB));
                      }// rxMB&gt;1024
                  }// rxKb &gt; 1024
              }// rxBytes&gt;=1024
  
              mHandler.postDelayed(mRunnable, 1000);
          }
      };
  
      @Override
      protected 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);
  
          }
      }
  }
  </code></pre>
  
  <p>i think there can be two workarounds for the issue , One would be to find some way of <strong>resetting</strong> trafficStats every second. other way would be to calculate it by subtracting the new value from the previous one every second. any help is highly appreciated</p>