Internet speed in Android programming

I want to write an application showing the speed of the Internet in the notification panel, I tried this code, but at the moment it shows only the link speed, not the real speed.

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

How can I get accurate internet access for Wi-Fi or 3G?

+1
android
source share
3 answers

At first I thought that you wanted you to be interested in the maximum speed of loading / unloading, for example, the information that Speedtest.net provides. Now I believe that you meant that you are interested in receiving current network traffic. I left an explanation regarding my initial assumption below.

New answer (if you are interested in receiving current upload / download traffic):

Take a look at the TrafficStats class. It has functions such as getMobileRxBytes() and getMobileTxBytes() , which give the number of bytes received and transmitted since loading, respectively. You can get these values ​​every second, then do calculations 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 upload / download speed):

This is not possible according to how you intend. Internet speed is the data transfer rate between your device and the destination server you have specified. When you go to Speedtest.net, you send information to your servers and receive from their servers as quickly as possible, and it tells you the speed that it detects. To be able to see speed statistics in real time, you need to constantly communicate with a remote server. (In addition, this server should be able to both load and load faster than the client, otherwise you will finish testing the maximum transmission speed of the Internet connection to the server, not the client!).

+1
source share

Try this code, I use it with my application

 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 ); 
+1
source share

You can get the answer nere: Software transmission of wifi / mobile network speed . There is no method that can directly return the real network speed.

0
source share

All Articles