1

I want to write an app that showing internet speed in notification bar, i tried this code but it only show the link speed not real speed at the moment

WifiInfo wifiInfo = wifiManger.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();

How can i get the exact internet speed throw wifi or 3G ?

Farzad Farazmand
  • 501
  • 1
  • 6
  • 13

3 Answers3

1

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!).

RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • `end up testing the speed of the server and not the client!`. But that is not what OP wants. Didn't you mean to say: `end up testing the speed of the server or the client and not the internet line!`. Both server and client should be able to handle data faster than can be transported to measure internet speed. – greenapps Nov 18 '14 at 20:00
  • Thanks, I've reworded a bit. – RogueBaneling Nov 18 '14 at 20:03
1

Try this code, Iam using it with my app

public class MainActivity extends AppCompatActivity {

final double [] RXOld = new double [1];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final Handler handler = new Handler();
handler.postDelayed(new Runnable() {


    @Override
    public void run() {


        ////////////////////////Code to be executed every second////////////////////////////////////////


        double overallTraffic = TrafficStats.getMobileRxBytes();

        double currentDataRate = overallTraffic - RXOld [0];

        TextView view1 = null;
        view1 = (TextView) findViewById(R.id.view1);
        view1.setText("Current Data Rate per second= " + currentDataRate);

         RXOld [0] = overallTraffic;

        handler.postDelayed(this, 1000);
    }
}, 1000 );
Ahmed
  • 93
  • 7
0

You can get an answer nere:Getting data speed of wifi/mobile network programatically. There's no method that can return directly the real speed of the network.

Community
  • 1
  • 1
mattd
  • 543
  • 4
  • 16