In this video it shows how to detect the mobile network which your Android phone is using.
It uses getDataNetworkType method of the TelephonyManager class to get the network type being currently available on the Android Phone. The details on this method is available in the below link:
https://developer.android.com/reference/android/telephony/TelephonyManager#getDataNetworkType()
Below is the list of the return values from the getDataNetworkType method. In our code using switch case, we have classified them for 2G, 3G, 4G and 5G network types.
NETWORK_TYPE_UNKNOWN, NETWORK_TYPE_GPRS, NETWORK_TYPE_EDGE, NETWORK_TYPE_UMTS, NETWORK_TYPE_CDMA, NETWORK_TYPE_EVDO_0, NETWORK_TYPE_EVDO_A, NETWORK_TYPE_1xRTT, NETWORK_TYPE_HSDPA, NETWORK_TYPE_HSUPA, NETWORK_TYPE_HSPA, NETWORK_TYPE_IDEN, NETWORK_TYPE_EVDO_B, NETWORK_TYPE_LTE, NETWORK_TYPE_EHRPD, NETWORK_TYPE_HSPAP, NETWORK_TYPE_GSM, NETWORK_TYPE_TD_SCDMA, NETWORK_TYPE_IWLAN, or NETWORK_TYPE_NR
This method needs the permission to READ_PHONE_STATE from the user, so respective permission checks are added in the java code and Manifest file of the project.
For any suggestions, queries, comments and appreciations, we will be glad to hear from you at:
https://programmerworld.co/contact/ or programmerworld1990@gmail.com
Screenshot of the layout:
Complete Source Code:
Java Code:
package com.example.mymobilenetworktype; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.TelephonyManager; import android.widget.TextView; import static android.telephony.TelephonyManager.NETWORK_TYPE_1xRTT; import static android.telephony.TelephonyManager.NETWORK_TYPE_CDMA; import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_0; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_A; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_B; import static android.telephony.TelephonyManager.NETWORK_TYPE_GPRS; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSDPA; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPAP; import static android.telephony.TelephonyManager.NETWORK_TYPE_IDEN; import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE; import static android.telephony.TelephonyManager.NETWORK_TYPE_NR; import static android.telephony.TelephonyManager.NETWORK_TYPE_UMTS; public class MainActivity extends AppCompatActivity { private TextView textView; private TelephonyManager telephonyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE}, PackageManager.PERMISSION_GRANTED); telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } switch (telephonyManager.getDataNetworkType()) { case NETWORK_TYPE_EDGE: case NETWORK_TYPE_GPRS: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_IDEN: case NETWORK_TYPE_1xRTT: textView.setText("2G"); break; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_HSPAP: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_EVDO_B: textView.setText("3G"); break; case NETWORK_TYPE_LTE: textView.setText("4G"); break; case NETWORK_TYPE_NR: textView.setText("5G"); break; default: textView.setText("Unknown"); } } }
Layout’s XML File:
<?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"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Manifest File:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mymobilenetworktype"> <uses-permission android:name="android.permission.READ_PHONE_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>