mobile - how can i determine internet speed in my android application -


in application want when click button show me exact internet speed. had read answer question has been asked in links determining internet speed in android , calculating internet speed in android , internet speed in android programming , on. didn't found answer exactly. used :

wifiinfo.getlinkspeed()

but show maximum speed. , use codes of this:

trafficstats

but didn't worked me. need show network speed (i.e 100 kbps ). how can this? please me.

to determine internet speed in android application first need download file onlineserver. why need download file? check average of internet download speed.

for need code 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 = 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 code download picture here

in doinbackground calculate 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 last thing calculate takentime subtracting endtime form start time , speed (size/takentime)

i hope works you


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -