Search code examples
androidtelephonymanager

How to retrieve SIM card IMSI in Android?


public String getSubscriberId(){
    operator = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    String IMSI = operator.getSubscriberId();
    return IMSI;
}

simID = (TextView) findViewById(R.id.text2);
    simIMSI = getSubscriberId().toString();

    if (simIMSI.equals("")){
        simID.setText("No SIM card detected!");
    }
    else{
        simID.setText(simIMSI.toString());
        SaveUniqueId(simIMSI.toString());
    }

I wish to retrieve the phone SIM card IMSI and display in a layout, I run the program using an emulator even though I know emulator does not have SIM card attached but it should have result like "No SIM card detected" right? But why I get error for this coding or is it something wrong in my "getSubscriberId()"?


Solution

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

    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;
    }
    

    For more details please refer this site http://ashnatechnologies.blogspot.in/2010/10/how-to-get-imei-on-android-devices.html