how can i determine internet speed in my android application
Asked Answered
L

3

6

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.

Lumpen answered 8/8, 2016 at 12:57 Comment(0)
S
6

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

Spathe answered 17/8, 2016 at 10:7 Comment(0)
C
0

WifiInfo.getLinkSpeed will give you the maximum speed of your current wifi network. Your actual wifi speed will be below this value, depending on how far you are to the access point, obstacles, interferences, ... This is your speed within your wifi network. If you access a device within your network, the transfer will be the minumum between your speed and the other device speed.

If you access the internet, then your wifi network is connected to internet and your speed will depend on the technology used (ADSL, cable, 4G, satellite,...) and the capacity of your internet provider (who is at the other side of your connection). Both values usually changein time, as they depend on usage patterns (number of concurrent users, network capacity,..)

Also bear in mind that, when connected to internet, you make requests to another device (most of the times to a server). The transfer speed you get will be the minimum between your speed & the one for the server you are contacting.

All in all, there are too many unkown elements to predict your internet speed.

However you can always calculate it, just make some requests, measure how many Kbytes (or Megabytes) you get, calculate how long it has taken and divide one by the other. This will be your current speed for that particular connection.

If you want more stable/trustable values, you should repeat this operation a number of times, and use the average. This is what speed tester (www.speedtest.net, testmy.net, ...) services do.

Counseloratlaw answered 8/8, 2016 at 13:42 Comment(1)
thanks a lot . but , can you tell me how to make requests and measure how many kbytes I get, how to calculate the time ? how can i do that? could you please help me with some code?Lumpen
V
0

To check the quality of the internet connection qualit and traffic. This library might help you..

Facebook - Network Connection Class

Network Connection Class is an Android library that allows you to figure out the quality of the current user's internet connection. The connection gets classified into several "Connection Classes" that make it easy to develop against. The library does this by listening to the existing internet traffic done by your app and notifying you when the user's connection quality changes. Developers can then use this Connection Class information and adjust the application's behaviour (request lower quality images or video, throttle type-ahead, etc).

Vex answered 8/8, 2016 at 14:5 Comment(2)
thanks dear , but could you help me how can i use this library? if its possible please show me in code .Lumpen
Kindly visit the github page, follow the instructions given on that page. To include library - you need to add this line in your gradle dependency to you app.compile 'com.facebook.network.connectionclass:connectionclass:1.0.1'.Vex

© 2022 - 2024 — McMap. All rights reserved.