01wifi01
01wifi01

Reputation: 415

Show Upload and Download Speed in android

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 current Upload and Download speed 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)

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 >= 1024) {
                // KB or more
                long rxKb = rxBytes / 1024;
                RX.setText(Long.toString(rxKb) + " KBs");
                if (rxKb >= 1024) {
                    // MB or more
                    long rxMB = rxKb / 1024;
                    RX.setText(Long.toString(rxMB) + " MBs");
                    if (rxMB >= 1024) {
                        // GB or more
                        long rxGB = rxMB / 1024;
                        RX.setText(Long.toString(rxGB));
                    }// rxMB>1024
                }// rxKb > 1024
            }// rxBytes>=1024

            // long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            // TX.setText(Long.toString(txBytes));
            long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            TX.setText(Long.toString(txBytes) + " bytes");
            if (txBytes >= 1024) {
                // KB or more
                long txKb = txBytes / 1024;
                TX.setText(Long.toString(txKb) + " KBs");
                if (txKb >= 1024) {
                    // MB or more
                    long txMB = txKb / 1024;
                    TX.setText(Long.toString(txMB) + " MBs");
                    if (txMB >= 1024) {
                        // GB or more
                        long txGB = txMB / 1024;
                        TX.setText(Long.toString(txGB));
                    }// rxMB>1024
                }// rxKb > 1024
            }// rxBytes>=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);

        }
    }
}

i think there can be two workarounds for the issue , One would be to find some way of resetting 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

Upvotes: 0

Views: 4298

Answers (2)

Dipen Patel
Dipen Patel

Reputation: 31

 private Handler mHandler = new Handler();
private long mStartRX = 0;
private long mStartTX = 0;
private final Runnable mRunnable = new Runnable() {
    public void run() {
        long resetdownload=TrafficStats.getTotalRxBytes();

        long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;

        download.setText(Long.toString(rxBytes));
        spdtstdownlod.setText(" bytes");
        if (rxBytes >= 1024) {

            long rxKb = rxBytes / 1024;

            download.setText(Long.toString(rxKb));
            spdtstdownlod.setText(" KBs");
            if (rxKb >= 1024) {

                long rxMB = rxKb / 1024;

                download.setText(Long.toString(rxMB));
                spdtstdownlod.setText(" MBs");
                if (rxMB >= 1024) {

                    long rxGB = rxMB / 1024;

                    download.setText(Long.toString(rxGB));
                    spdtstdownlod.setText(" GBs");
                }
            }
        }

        mStartRX=resetdownload;

        long resetupload=TrafficStats.getTotalTxBytes();

        long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;

        upload.setText(Long.toString(txBytes));
        spdtstupload.setText(" bytes");
        if (txBytes >= 1024) {

            long txKb = txBytes / 1024;

            upload.setText(Long.toString(txKb));
            spdtstupload.setText(" KBs");
            if (txKb >= 1024) {

                long txMB = txKb / 1024;

                upload.setText(Long.toString(txMB));
                spdtstupload.setText(" MBs");
                if (txMB >= 1024) {

                    long txGB = txMB / 1024;

                    upload.setText(Long.toString(txGB));
                    spdtstupload.setText(" GBs");
                }
            }
        }

        mStartTX=resetupload;

        mHandler.postDelayed(mRunnable, 1000);
    }
};

Upvotes: 3

Vatsal Shah
Vatsal Shah

Reputation: 547

You find the connection speed programmatically in Android as follows:

WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);

    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
    }

Note that you should use the ConnectivityManager to state your preferred network if you are anticipating use of wifi over other available networks, otherwise the speed may not actually be in use

UPDATE

Please check the below code which help you

public static void Traffic(Context context){
  if (applications == null)   applications=loadAppInfomation(context);
  Iterator<AppInfo> iterator=applications.iterator();
  double temp_upload=0, temp_download=0;
  while (iterator.hasNext()) {
    AppInfo temp=iterator.next();
    temp_upload=TrafficStats.getUidTxBytes(temp.getUid()) / 1024.0;
    temp_download=TrafficStats.getUidRxBytes(temp.getUid()) / 1024.0;
    if (temp_upload <= 0)     temp_upload=0;
    if (temp_download <= 0)     temp_download=0;
    temp.setUL(temp_upload);
    temp.setDL(temp_download);
  }
}

Upvotes: 0

Related Questions