Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get Device IMEI and ESN Programmatically in Android.
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. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
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.
XML
<? xml version = "1.0" encoding = "utf-8" ?> android:id = "@+id/idRLContainer" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!-- in the below line, we are creating a text view for heading --> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/idTVIMEI" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:text = "Device IMEI in Android" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> <!-- in the below line, we are creating a text view for displaying imei --> < TextView android:id = "@+id/idTVIMEI" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> </ RelativeLayout > |
Step 3: Working with the MainActivity File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import android.telephony.TelephonyManager import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat class MainActivity : AppCompatActivity() { // in the below line, we are creating variables. private val REQUEST_CODE = 101 private lateinit var imei: String override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // in the below line, we are initializing our variables. val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager val imeiTextView = findViewById<TextView>(R.id.idTVIMEI) // in the below line, we are checking for permissions if (ActivityCompat.checkSelfPermission( this , Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // if permissions are not provided we are requesting for permissions. ActivityCompat.requestPermissions( this , arrayOf(Manifest.permission.READ_PHONE_STATE), REQUEST_CODE) } // in the below line, we are setting our imei to our text view. imei = telephonyManager.imei imeiTextView.text = imei } // in the below line, we are calling on request permission result method. override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { super .onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == REQUEST_CODE) { // in the below line, we are checking if permission is granted. if (grantResults.isNotEmpty() && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) { // if permissions are granted we are displaying below toast message. Toast.makeText( this , "Permission granted." , Toast.LENGTH_SHORT).show() } else { // in the below line, we are displaying toast message if permissions are not granted. Toast.makeText( this , "Permission denied." , Toast.LENGTH_SHORT).show() } } } } |
Java
import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.TelephonyManager; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; public class MainActivity extends AppCompatActivity { // in the below line, we are creating variables final int REQUEST_CODE = 101 ; String imei; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // in the below line, we are initializing our variables. TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); TextView imeiTextView = findViewById(R.id.idTVIMEI); // in the below line, we are checking for permissions if (ActivityCompat.checkSelfPermission( this , Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // if permissions are not provided we are requesting for permissions. ActivityCompat.requestPermissions( this , new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE); } // in the below line, we are setting our imei to our text view. imei = telephonyManager.getImei(); imeiTextView.setText(imei); } // in the below line, we are calling on request permission result method. @Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults) { super .onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CODE) { // in the below line, we are checking if permission is granted. if (grantResults.length != 0 && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) { // if permissions are granted we are displaying below toast message. Toast.makeText( this , "Permission granted." , Toast.LENGTH_SHORT).show(); } else { // in the below line, we are displaying toast message // if permissions are not granted. Toast.makeText( this , "Permission denied." , Toast.LENGTH_SHORT).show(); } } } } |
Step 4: Adding the below Permissions to AndroidManifest.xml
Navigate to the AndroidManifest.xml file and add the below permissions to it in the manifest tag.
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />