get Download Speed - Android java.lang

Android examples for java.lang:Double

Description

get Download Speed

Demo Code

import android.content.Context;
import java.text.DecimalFormat;

public class Main{

    public static String getDownloadSpeed(int len, long time) {
        String str = "0k/s";
        if (time == 0)
            return str;
        float sec = time / 1000;
        float speed = len / 1024 / sec;

        if (speed > 1000) {
            str = new DecimalFormat("0.00").format(speed / 1024) + "m/s";
            return str;
        } else {/*w  w w .ja  v a2s  .  c  o  m*/
            str = ((int) speed) + "k/s";
            return str;
        }
    }

}

Related Tutorials