check user connection's bandwidth in android mobile app at runtime -


i working on android application need play find connection's bandwidth @ run time. found out solution on stack overflow saying can download file server , calculating size vs time , can speed of connection.

check bandwidth rate in android

is best way (only way) accurate results ? sharing knowledge.

you can't query information. internet speed determined , controlled isp, not network interface or router.

so way can (current) connection speed downloading file close enough location , timing how long takes retrieve file. example:

static final string file_url = "http://www.example.com/speedtest/file.bin"; static final long file_size = 5 * 1024 * 8; // 5mb in kilobits  long mstart, mend; context mcontext; url murl = new url(file_url); httpurlconnection mcon = (httpurlconnection)murl.openconnection(); mcon.setchunkedstreamingmode(0);  if(mcon.getresponsecode() == httpurlconnection.http_ok) {     mstart = new date().gettime();      inputstream input = mcon.getinputstream();     file f = new file(mcontext.getdir("temp", context.mode_private), "file.bin");     fileoutputstream fo = new fileoutputstream(f);     int read_len = 0;      while((read_len = input.read(buffer)) > 0) {         fo.write(buffer, 0, read_len);     }     fo.close();     mend = new date().gettime();     mcon.disconnect();      return file_size / ((mend - mstart) / 1000); } 

this code, when sightly modified (you need mcontext valid context) , executed inside asynctask or worker thread, download remote file , return speed in file downloaded in kbps.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -