title: Android Telephone manager to detect sim
body: <p>I am working on an Android auto-start app that's basically dependent on SIM card state.  When my app auto starts I need it to check where the SIM card has been changed or not.  After that I compare the current SIM with the past SIM by obtaining the shared preference.  But the app returns a null pointer exception when getting the new SIM card's value.</p>
  <p>I want to react of SIM states.</p>
  <p>When I receive the <code>SIM_STATE_READY</code> state I want to get the new SIM state from telephone manager.</p>
  <pre><code>  telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
       int simState = telMgr.getSimState();
      
       switch (simState) 
      {
          case (TelephonyManager.SIM_STATE_ABSENT):
              System.out.println(&quot;*******************************************Sim State absent******************************&quot;);
              break;
          case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): 
              System.out.println(&quot;*******************************************SIM_STATE_NETWORK_LOCKED******************************&quot;+sim);
              break;
          
          case (TelephonyManager.SIM_STATE_PIN_REQUIRED): 
              System.out.println(&quot;*******************************************SIM_STATE_PIN_REQUIRED******************************&quot;+sim);
          break;
          case (TelephonyManager.SIM_STATE_PUK_REQUIRED): 
              System.out.println(&quot;*******************************************SIM_STATE_PUK_REQUIRED******************************&quot;+sim);
          break;
          case (TelephonyManager.SIM_STATE_UNKNOWN): 
              System.out.println(&quot;*******************************************SIM_STATE_UNKNOWN******************************&quot;+sim);
          break;
          case (TelephonyManager.SIM_STATE_READY): 
          {
      
          }
      break;
      }
      default: break;
      }
  </code></pre>
  <p>I'm doing this but don't know how to listen for SIM the states I want when the SIM is ready so that I can then execute some code.  When the device boots up it always returns &quot;SIM_STATE_UNKNOWN&quot; and causes program's execution got complete.</p>