How to get IMEI on Android Devices?

Hi Readers,

As per your queries about IMEI on Android devices; I am posting this article, please do let me know with your feedback and further queries. The solution is as follows:-

Device, network, SIM & data state related info on Android platform is managed by TelephonyManager. Your application can use the methods defined in this class to determine the desired information. Please note, you cannot instantiate this class directly; instead you need to retrieve a reference to an instance like this:-

String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);

To access some telephony information you need to set appropriate permission in your application manifest file, because few informations are permission protected. In our case we need to set permission for our app:-

Now let's start with actual source code to retrieve the information:-

public String findDeviceID() {
     String deviceID = null;
     String serviceName = Context.TELEPHONY_SERVICE;
     TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
     int deviceType = m_telephonyManager.getPhoneType();
     switch (deviceType) {
           case (TelephonyManager.PHONE_TYPE_GSM):
           break;
  case (TelephonyManager.PHONE_TYPE_CDMA):
  break;
  case (TelephonyManager.PHONE_TYPE_NONE):
  break;
          default:
         break;
     }
     deviceID = m_telephonyManager.getDeviceId();
     return deviceID;
}

The above method will return you the IMEI in-case of GSM and MEID or ESN for CDMA devices. When you can run this code on real device the output will be like this:-
Point to be noted here is deviceType that we are getting using TelephonyManager Class. This code will work on Android 1.6 and above because CDMA stack is implemented on Android 1.6 and above. So If you are using Android 1.5 devices just remove CDMA related code from it.

Using TelephonyManager you can retrieve much more information about device. For example:-

Retreive MSISDN:-
String msisdn =  m_telephonyManager.getLine1Number();
Retreive SIM Serial Number:-
String simSerialNumber = m_telephonyManager.getSimSerialNumber();
Retreive IMSI:-
String imsi = m_telephonyManager.getSubscriberId();

For more info please visit TelephonyManager

Comments

  1. I found it helpfull and great piece of information.

    ReplyDelete
  2. Thanks, I appreciate your time & will try to share more interesting articles.

    Best Regards,
    Ashish

    ReplyDelete
  3. Thank you for the interesting info. I am able to successfully get the IMEI value in to the string variable. However, when I try to perform logic with the variable, it does not seem to work correctly. For example:

    deviceID = 123456789 (I know this is not valid... just for example) as result of executing the code so the variable deviceID actually contains the IMEI of the device.

    Now I want to do a programmatic comparison to a fixed value. In my code I include:

    String compareIMEI = "123456789";

    if (deviceID == compareIMEI)
    alertbox.show();

    Even though the assigned value of compareIMEI variable appears to be the same as the content value of deviceID, the if statement always fails. Suggestions?

    ReplyDelete
  4. Hi,

    Just now, I tried to compare two different IMEI and also the same IMEI too. I am able to see the result. I compare using:-

    if (deviceID.equals(compareDeviceID)) {
    Toast.makeText(getBaseContext(), "Your device ID is = " + deviceID, Toast.LENGTH_LONG).show();
    }

    Please put alert for both successful & failure comparison. Also append both IMEI values to alert, which will help you in debugging.

    Best Regards,
    Ashish

    ReplyDelete
  5. Thank you very much, RESPECT :))))))))))))

    ReplyDelete
  6. Is it possible to get IMEI number for Android Tablet also, please let me know and Thank's for u'r concern.

    ReplyDelete
  7. Yep, IMEI should be available on every Android Tablet. I tried on Samsung Galaxy Tab and able to get the IMEI number. Especially I am sure for all tablet with SIM card support.

    ReplyDelete
  8. I want to do sim checker application when you change your Sim card that time it check on Device StartUp previous simSerial and New Serial. If Sim card has been changed that time it send the Message? Please Help me. This Question is Affacted on my job. Plz... Plz..Plz.

    ReplyDelete
  9. You need to write a service that run in background and detect SIM change using IMSI number. TO check at the device start up, please go through with this link...

    http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html

    You need to call SIM change service at onStartCommand() for platform Android 2.0 & above. Pre 2.0 platform you need to use onStart().

    Best Regards,
    Ashish Sharma.

    ReplyDelete
  10. Great piece of work Ashish. I am new to android development and really want to understand how it is done from scratch. How do I integrate it to get same result.

    ReplyDelete
  11. This was what I was looking for. Thank you

    ReplyDelete

Post a Comment