Android get imei number programmatically

23 Apr

IMEI numbers are used to identify the mobile device. It is a unique identification of your mobile device. It is to prevent theft.
with the IMEI number network operator can trace the mobile device.

In order to get imei number in your android app. first of all your need add a new permission in your AndroidManifest.xml file. Add below line in your AndroidManifest.xml file.


Then you can write code to get IMEI number.


public static String getUniqueIMEIId(Context context) {
try {

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei;

if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
imei = telephonyManager.getImei();
} else {
imei = telephonyManager.getDeviceId();
}

if (imei != null && !imei.isEmpty()) {
return imei;
} else {
return android.os.Build.SERIAL;
}
}

} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
return errors.toString();
//e.printStackTrace();
}

return "not_found";
}


This function can be placed in global settings file. so you can use it anywhere in the application.

Dual SIM phone have two IMEI numbers. So which one will be returned.

Every Dual sim phone have two IMEI numbers, so which one will be returned this is the most important question.
The return of IMEI Number can be different if user is taking on the phone.
If user is using first sim then the second IMEI number will be returns and if user is using second sim then the first imei number will be returns.
Your phone can return any one IMEI number according to the condition of the phone. So it is recommended that you should handle both IMEI number for Dual SIM phone.

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen + 16 =