I am working on an app which uploads a large amount of data. I want to determine the transfer rate of the upload, to show in a notification.
- One post suggests using the
WifiInfo
which will not work for mobile data. - Another post suggests getting the network type to estimate the speed.
I'm not satisfied with the answers in these posts, so I am asking again.
I've seen apps which display the upload transfer rate, as well as some custom ROMs like Resurrection Remix.
How can I determine the transfer rate of these uploads?
It is feasible to obtain the transferred traffic amount using
android.net.TrafficStats
. Here is an implementation of this idea which measures the up-stream and down-stream transfer rate. You can measure the rate of mobile network by passingTrafficSpeedMeasurer.TrafficType.MOBILE
to theTrafficSpeedMeasurer
constructor, otherwise usingTrafficSpeedMeasurer.TrafficType.ALL
will result in measuring general traffic (WiFi/Mobile). Also by settingSHOW_SPEED_IN_BITS = true
inMainActivity
you can change the unit of speed measuring tobit
s per second.MainActivity.java
TrafficSpeedMeasurer.java
ITrafficSpeedListener.java
Utils.java
. Visual Result