How to get the device s IMEI ESN programmatically in android

0 votes
I want to use the IMEI to individually identify each device (or ESN number for CDMA devices). How can I programmatically access this?
Nov 16, 2022 in Android by Edureka
• 12,690 points
1,405 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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" />
answered Nov 17, 2022 by Edureka
• 13,620 points

edited Mar 5

Related Questions In Android

0 votes
0 answers

How do I get the current GPS location programmatically in Android?

I need to get my current location ...READ MORE

Nov 23, 2022 in Android by Ashwini
• 5,430 points
794 views
0 votes
1 answer

How to turn on front flash light programmatically in Android?

For this problem you should: Check whether the ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
3,661 views
0 votes
0 answers

How to get current time and date in Android?

How can I get the current time ...READ MORE

Nov 22, 2022 in Android by Edureka
• 12,690 points
599 views
0 votes
0 answers

How to implement the system lock screen in your own android app?

How should the system lock screen be ...READ MORE

Nov 22, 2022 in Android by Edureka
• 12,690 points
842 views
0 votes
0 answers
0 votes
0 answers

How to make grid view scroll horizontally not vertically in android?

I use a flexible Grid View. means ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
701 views
0 votes
0 answers

Android : Check whether the phone is dual SIM

After extensive investigation on discussion boards, I now understand that a dual SIM phone's IMSI or SIM serial numbers cannot be found (except for contacting the manufacturer).  My original query has been amended to: Can we even tell the phone has two SIMs?  I think someone with some intellect could find it.  I can think of a few ways, including: tracing the logs for the IMEI number and dialling a USSD code (I tested this in India with *139#).  It succeeded.)  This will provide me with the SIM's IMEI number from which I called the USSD code.  (The phone is assumed to adhere to Android standards and have two IMEI numbers.) preserving the SIM's IMSI and/or serial number. ...READ MORE

Dec 10, 2022 in Android by sarit
• 1,830 points
868 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
3,954 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
2,601 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! here’s a barcode scanner written in six ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,138 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP