How to Get the Phone IMEI


Another common requirement for many applications is to get a unique Id you can use to identify the user. Most applications, on all platforms, use the IMEI rather than the telephone number (MSISDN) because the SIM isn’t guaranteed to contain a MSISDN (or the correct MSISDN) - I believe this depends on the specification when the SIM is manufactured.

Under Android, it’s easy to get the IMEI. There’s an example in the OS Source in android.provider.Settings

String imei = TelephonyManager.getDefault().getDeviceId();
if (TextUtils.isEmpty(imei)) {
   return "";
}

The comments in android.provider.Settings explain that if the TelephonyManager isn’t ready then you will get an empty string. In the example above,  the code returns an empty string if this happens.

In order to be able to fetch the IMEI you will need to declare the following in your manifest…

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

This might cause your end users to question what your application is actually doing. If you don’t like this then you might like to consider using the system settings Android_ID as described at strazzere.com.

Comments are closed.