Did you ever spend hundreds of bucks to get something done on the android device that you couldn’t do for not having the proper knowledge? Or have you wasted countless hours sitting confused trying to figure out how to get it done?
Well, you don’t have to worry anymore. You have stumbled upon the right people. Today, we will find out how to get IMEI in android programmatically.
IMEI, also known as International mobile equipment identity, is a unique number that every mobile in the world has. It is usually used to manage and identify phones. Unfortunately, the correct process of finding IMEI programmatically is very less discussed.
As a result, many of us find it hard to get IMEI on android. So this guide is all about learning a few different ways of getting it manually with step-by-step guide.
Step by Step Guide on How to Get IMEI in Android Programmatically:
So for the first way, we are going to be using TelephonyManager. It provides access to information about the telephony services on the device. There are two ways of getting IMEI using TelephonyManager and we will be discussing both.
So let’s get straight into it:
Step-1:
Call android.telephony.TelephonyManager.getDeviceId().
It would look into its database and return whichever unique numbers identify the device. It usually is IMEI for GSM and MEID for CDM.
Step2:
You need to use the following permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This has to be done if you want this process to work.
Step3:
You should also add the following code in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
We recommend adding this because in the emulator, you might get a value like “0000….”. As getDeviceId() returns null if the device Id is not available.
Moreover, you can also use the following code to get IMEI on your android device programmatically.
Step1:
Enter the code given below:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String device_id = tm.getDeviceId();
Step2:
Add the following permission in Android Manifest:
android.permission.READ_PHONE_STATE
This is more like the way easier way of getting your IMEI without having to ask anyone or pay anyone to do it.
How to Get IMEI Number in Android Studio Programmatically
So, for the second way, we are going to be working with Android Studio. In a nutshell, it is an android device’s official IDE. The main objective for android studio is to accelerate your development and aid you in building a good quality app. Today we are going to be using it to get our android device’s IMEI.
Follow the steps given below:
Step1:
Start android studio and create a new project. For that go to Files⇒ New project and fill in all the required details to create a new project.
Step2:
Add the following code to res/layout/activity_main.xml.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:text="Check IEMI number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="@id/textView"
android:layout_marginBottom="20sp"/>
<TextView
android:id="@+id/textView"
android:textSize="16sp"
android:hint="Your IEMI number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Step3:
So, for the next step, we are going to add the following code to src/MainActivity.java
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
TextView textView;
String IMEINumber;
private static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE);
return;
}
IMEINumber = telephonyManager.getDeviceId();
textView.setText(IMEINumber);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission denied.", Toast.LENGTH_SHORT).show();
}
}
}
}
Step4:
Then lastly, we are going to add this to androidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
<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>
By doing this, we have created an application that will help us get our IMEI. Hopefully, you have connected your actual mobile device with your computer. As for running this, start the app from android studio, open one of your project’s activity files and click the “Run” icon from the toolbar.
Select your mobile device as an option and then check your display. What you should be seeing is:
And here you go, we just created an app to see your device’s IMEI for free!
Frequently Asked Questions (F.A.Q’s)
Can Android 10 get programmatically IMEI number?
Yes, It can. But as for starting android 10, apps must have
READ_PRIVILEGED_PHONE_STATE privileged permission for accessing the device’s non-changeable identifiers. This includes both IMEI and serial numbers.
Is IMEI number unique?
Yes, it is. Think of it as your phone’s face id. It is a 15 digit number completely unique to your phone. Indeed, phone manufacturers and carriers share IMEI to enable tracking of stolen or compromised phones.
Can android apps read IMEI?
Not always. In order to do so, you must grant the app READ_PHONE_STATE permission. Only then, the app will be able to access the IMEI.
How can I get my IMEI number?
If you want to find your IMEI normally without programming, go to settings. Then scroll down and select about the phone. Scroll down to the bottom to find your IMEI number.
How to find IMEI number without my phone?
In case you lost your mobile device but did not copy your IMEI number, you can still retrieve it from your google accounts. Just go to google settings, sign in and expand the android tab.
Here, you can see you have connected every device to your google account. All the IMEI numbers of the devices connected will be right there as well.
Conclusion
Hopefully, you’ve learned how to get IMEI in android programmatically. If not, then go over the article and give that a read once again. The processes mentioned above are by far the easiest way of finding IMEI with minimum requirements. The steps mentioned above almost work on every android version so don’t worry we got you covered.
No matter whichever process you choose to follow, just make sure you do every step correctly without any error to get IMEI on your android device.