Reputation: 1009
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.
Upvotes: 6
Views: 8064
Reputation: 429
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
Upvotes: 6
Reputation: 137
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).
Upvotes: 0
Reputation: 99
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.
Upvotes: 0