Android get imei number programmatically

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 numnber.


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 can be placed in global settings file. so you can used in anywhere in the application.

Thanks

Leave a Reply

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

fifteen − 5 =