Skip to content
Related Articles

Related Articles

Programmatically Check the Network Speed in Android

Improve Article
Save Article
  • Last Updated : 17 Oct, 2020
Improve Article
Save Article

Network speed can be defined as the total number of packets being exchanged by the client and the server per second, usually calculated in Megabits per second(Mbps). A bit is either a 0 or 1. 1 Megabit implies 1 Million bits. Usually, packet size depends on the protocol and various other factors and usually ranges on a vast scale. In this article, a program is implemented that fetches us Upload and Download speeds with the server. Note that the program is successful when the device is connected to a network.

This program could be used for research as well as for optimization:

  • Research: one could derive the content and the available speeds to correlate the network dependency.
  • Optimization: one could continuously monitor the speed and context, to avoid the connection when not required or reduce the quality of a video being broadcasted depending upon the current speeds.


Step 1: Create an Empty activity in Android Studio. To create one, follow this article- Check if the primary language selected is Kotlin.

Step 2: Go to the AndroidManifest.xml file and add the uses-permission ACCESS_NETWORK_STATE.


<?xml version="1.0" encoding="utf-8"?>
  <!--Add this permission-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Step 3: In the activity_main.xml, add a button. Bellow is the code for the same.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    <!--Create a Button-->
        android:text="Show Speed"

Step 4: In the MainActivity.kt, add the below code. setOnClickListener is added with the button, which when clicked shows the upload speed and download speed as a toast on the screen.


package org.geeksforgeeks.networkspeed
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring Button from the layout file
        val btn = findViewById<Button>(
        // Action when the button id clicked
        btn.setOnClickListener {
            // Connectivity Manager
            val cm = applicationContext.getSystemService
          (Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            // Network Capabilities of Active Network
            val nc = cm.getNetworkCapabilities(cm.activeNetwork)
            // DownSpeed in MBPS
            val downSpeed = (nc.linkDownstreamBandwidthKbps)/1000
            // UpSpeed  in MBPS
            val upSpeed = (nc.linkUpstreamBandwidthKbps)/1000
            // Toast to Display DownSpeed and UpSpeed
                           "Up Speed: $upSpeed Mbps \nDown Speed: $downSpeed Mbps",


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!