e.hadid e.hadid - 3 months ago 15
Android Question

how can i determine internet speed in my android application

In my application I want that when i click a button it show me the exact internet speed.
I had read all answer about this question which has already been asked in this links determining internet speed in android , Calculating Internet Speed in android , Internet Speed in android programming and so on.
But I didn't found my answer exactly. I used this :


WifiInfo.getLinkSpeed()


but it show the maximum speed. and also i use the codes of this:


TrafficStats


but it didn't worked for me. i need to show my network speed (i.e 100 kbps ). how can i do this?
Please help me.

Answer

To determine internet speed in an android application first you need to download some file from onlineserver. Why we need to download file? to check the average of internet download speed.

for that you need to code like this

 private class InternetSpeedTest
        extends AsyncTask<String, Void, String> {

    long startTime;
    long endTime;
    private long takenTime;

    @Override
    protected String doInBackground(String... paramVarArgs) {

        startTime = System.currentTimeMillis();
        Log.d(TAG, "doInBackground: StartTime" + startTime);

        Bitmap bmp = null;
        try {
            URL ulrn = new URL(paramVarArgs[0]);
            HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);

            Bitmap bitmap = bmp;
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 99, stream);
            byte[] imageInByte = stream.toByteArray();
            long lengthbmp = imageInByte.length;

            if (null != bmp) {
                endTime = System.currentTimeMillis();
                Log.d(TAG, "doInBackground: EndTIme" + endTime);
                return lengthbmp + "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(String result) {

        if (result != null) {
            long dataSize = Integer.parseInt(result) / 1024;
            takenTime = endTime - startTime;
            double s = (double) takenTime / 1000;
            double speed = dataSize / s;
            Log.d(TAG, "onPostExecute: " + "" + new DecimalFormat("##.##").format(speed) + "kb/second");
        }
    }

In this code will download a picture from Here

In doInBackground you will calculate the size of Image after download completed here

Bitmap bitmap = bmp;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 99, stream);
        byte[] imageInByte = stream.toByteArray();
        long lengthbmp = imageInByte.length

and the last thing is to calculate takenTime by subtracting endtime form start time and the speed is (size/takenTime)

I hope it works for you