We frequently need a unique identity to identify specific mobile consumers when developing Android applications. We make use of a special identity or address to identify that user. We can utilize the android device id to create that special identity. We'll look at How to Get Device IMEI and ESN Programmatically in Android in this article.
Step-by-Step Execution
Step 1: Open Android Studio and create a new project.
Please refer to How to Create/Start a New Project in Android Studio for instructions on how to do so. Both Java and the Android programming language Kotlin have been provided with the code for it.
Step 2: XML Files to Work With
To add the following code to activity main.xml, go to the app > res > layout > activity main.xml. The code for the activity main.xml file is shown below.
<?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: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: Using the MainActivity File
Refer to the following code in the MainActivity File. The MainActivity File's source code is provided below. Inside the code, comments are added to help the reader comprehend it better.
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: Include the permissions listed below in AndroidManifest.xml.
Go to the AndroidManifest.xml file and edit the manifest tag to include the following permissions.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />