How to Find Out Carrier’s Name in Android Programmatically?
In this article we will see how to retrieve the carrier name on Android device. This information can be useful for applications that need to provide specific functionality based on the user’s cellular network provider. A sample video is given below to get an idea about what we are going to do in this article.
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. Note that select Kotlin as the programming language.
Step 2: Adding Permissions
Navigate to app > manifests > AndroidMainfest.xml and add this
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This permission declaration in Android manifest file specifies that the app requests permission to access the phone state which includes information about the device cellular network like carrier name.
Step 3: Creating Layout
We have created normal ui by adding one textview,one button and one Spinner for selecting the sim slot
activity_main4.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout 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:background = "@color/white" android:layout_height = "match_parent" android:gravity = "center" tools:context = ".MainActivity4" > < androidx.cardview.widget.CardView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "16dp" app:cardCornerRadius = "8dp" > < RelativeLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:padding = "16dp" > < TextView android:id = "@+id/tv" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hello World!" android:textSize = "24sp" /> < Button android:id = "@+id/btnSubmit" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@id/tv" android:layout_marginTop = "26dp" android:text = "Submit" /> < Spinner android:id = "@+id/spin" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@id/btnSubmit" android:layout_marginTop = "16dp" /> </ RelativeLayout > </ androidx.cardview.widget.CardView > </ RelativeLayout > |
UI Output:
Step 4: Working on Kotlin File
Code retrieves and displays the carrier name for a selected SIM slot that is selected from a spinner after requesting the necessary permissions.
Kotlin
package com.ayush.gfgapp import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.os.Bundle import android.telephony.SubscriptionManager import android.widget.ArrayAdapter import android.widget.Button import android.widget.Spinner import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat class MainActivity4 : AppCompatActivity() { // Declaring UI elements private lateinit var spin: Spinner private lateinit var tv: TextView private lateinit var btnSubmit: Button override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main4) // Initializing UI elements from our xml file tv = findViewById(R.id.tv) spin = findViewById(R.id.spin) btnSubmit = findViewById(R.id.btnSubmit) // Seting a click listener for the submit button btnSubmit.setOnClickListener { getCarrierName() } // Defining options for the Spinner val slots = arrayOf( "1" , "2" ) // Creating an ArrayAdapter for the Spinner val _spinAdapter = ArrayAdapter( this , android.R.layout.simple_spinner_dropdown_item, slots) // Seting the ArrayAdapter on the Spinner spin.adapter = _spinAdapter // Requesting permission to read phone state ActivityCompat.requestPermissions( this , arrayOf(Manifest.permission.READ_PHONE_STATE), PackageManager.PERMISSION_GRANTED) } private fun getCarrierName() { // Geting the SubscriptionManager val susbsManager = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager // Checking if the app has the required permission (that is Manifest.permission.READ_PHONE_STATE) if (ActivityCompat.checkSelfPermission( this , Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // If permission is not granted, display a message tv.text = "Permission not granted" return } // Retrieving the list of active subscriptions val subsManagerList = susbsManager.activeSubscriptionInfoList try { // Geting the selected slot number from the Spinner val selectedSlot = spin.selectedItem.toString().toInt() // Retrieving information about the subscription in the selected slot val subscriptionInfo = subsManagerList[selectedSlot - 1 ] // Geting the carrier name or display "Unknown" if not available val stringCarrierName = subscriptionInfo.carrierName?.toString() ?: "Unknown" // Displaying the carrier name in text view tv.text = stringCarrierName } catch (e: Exception) { tv.text = "Sim in slot ${spin.selectedItem.toString()} not available" } } } |
Output:
Contact Us