How to Detect Cellular Network Type (2G, 3G, 4G and 5G) in Android?
Android devices like mobiles and tablets are available with SIM card slots. Users can use them to connect to a particular network. Over time, telecommunication networks have changed rapidly giving users the extent of a new band with higher data speed. Devices are manufactured according to the advancements in available network technology. So as the title suggests, in this article, we will show you how you could programmatically fetch the type of network the device is connected to in Android. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with AndroidManifest.xml file
As we need to access the network state to figure out type of network, we need to add the below permission in the AndroidManifest.xml file to execute our code.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "org.w3wiki.cellularnetworkband" > <!--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/Theme.CellularNetworkBand" > < activity android:name = ".MainActivity" android:exported = "true" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a button as shown below. We will use this button to check the network type.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < Button android:id = "@+id/button_1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "click" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.w3wiki.cellularnetworkband import android.content.Context import android.net.ConnectivityManager import android.os.Bundle import android.telephony.TelephonyManager import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing // the button form the layout file val mButton = findViewById<Button>(R.id.button_1) // When button is clicked, function is called // that returns a string displaying type of network // This string is displayed in a Toast message mButton.setOnClickListener { val message = mGetNetworkClass(applicationContext) Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show() } } // Function to find out type of network private fun mGetNetworkClass(context: Context): String? { // ConnectionManager instance val mConnectivityManager = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager val mInfo = mConnectivityManager.activeNetworkInfo // If not connected, "-" will be displayed if (mInfo == null || !mInfo.isConnected) return "-" // If Connected to Wifi if (mInfo.type == ConnectivityManager.TYPE_WIFI) return "WIFI" // If Connected to Mobile if (mInfo.type == ConnectivityManager.TYPE_MOBILE) { return when (mInfo.subtype) { TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_GSM -> "2G" TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_TD_SCDMA -> "3G" TelephonyManager.NETWORK_TYPE_LTE, TelephonyManager.NETWORK_TYPE_IWLAN, 19 -> "4G" TelephonyManager.NETWORK_TYPE_NR -> "5G" else -> "?" } } return "?" } } |
Output:
We tested the application when it was connected to a 4G network. The toast displayed “4G” in the output on button click.
Contact Us