I am working on an android app that uses internet connectivity to work.I want to monitor the network speed using an indicator which will be red for poor
connection, yellow for fair
and green for good
strength, and show it on the status bar so that the user may know the network speed all the time while using the app.I came across TrafficStats library in which I will get the no. of bytes transmitted using TrafficStats.getMobileTxbytes()
and no. of bytes received using TrafficStats.getMobileRxbytes()
but now I have a query to deal with.
- TrafficStats would give network speed which is based on current data transfer. So if nothing is transferred in say last 10 seconds, it would return 0kbps speed and our indicator would be red, whereas actual network was good and it should have been green.
I am just a newbie in Android, and want some insights here.
Also is there some other good way to solve this problem?
Unfortunately, such data can only be estimated. There is no API in Android, that gives you an average speed in a specified amount of time.
Here is, what I made for specifying the average speed, based on the mobile network connection type (and the unit in external function):
However, the code above will only work for mobile connection. When the WiFi is on, the method would be different: