Open In App

Programmatically Check the Network Speed in Android

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:



Approach:

Step 1: Create an Empty activity in Android Studio. To create one, follow this article- https://www.geeksforgeeks.org/android-how-to-create-start-a-new-project-in-android-studio/. 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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.geeksforgeeks.networkspeed">
 
  <!--Add this permission-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <!----------------------->
   
  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!--Create a Button-->
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Speed"
        android:layout_centerInParent="true"
        />
 
</RelativeLayout>

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.net.ConnectivityManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
 
class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Declaring Button from the layout file
        val btn = findViewById<Button>(R.id.btn)
 
        // 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
            Toast.makeText(applicationContext,
                           "Up Speed: $upSpeed Mbps \nDown Speed: $downSpeed Mbps",
                           Toast.LENGTH_LONG).show()
        }
    }
}

Output:


Article Tags :