At first, I thought you meant you were interested in seeing maximum download/upload speed, such as the info that Speedtest.net provides. Now I believe you meant you are interested in obtaining the current network traffic usage. I've left the explanation relating to my original assumption at the bottom.
New answer (if you are interested in obtaining current upload/download traffic):
Look into the TrafficStats
class. This has functions such as getMobileRxBytes()
and getMobileTxBytes()
which give the number of bytes received and transmitted since boot, respectively. You can get these values every second, then do a calculation to find the difference per second (or, "bytes per second").
// set this to true when you want it to stop
boolean mStopHandler = false;
Runnable runnable = new Runnable() {
@Override
public void run() {
// complete calculations
if (!mStopHandler) {
mHandler.postDelayed(this, 1000); //runs every second
}
}
};
// begin task
mHandler.post(runnable);
Original answer (assuming you are interested in maximum download/upload speed):
This is not possible in the way that you intend. The internet speed is the rate of data transfer between your device and a destination server that you specify. When you go to Speedtest.net, you are sending information to their servers and receiving from their servers as fast as possible, and it tells you the rate that it is detecting. To be able to see real-time speed statistics, you would need to be constantly communicating with a remote server. (Additionally, this server would need to be able to both download and upload faster than the client could, otherwise you end up testing the maximum transfer rate of the server's internet connection, and not the client's!).