Android Telephone manager to detect sim
<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("*******************************************Sim State absent******************************");
            break;
        case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): 
            System.out.println("*******************************************SIM_STATE_NETWORK_LOCKED******************************"+sim);
            break;

        case (TelephonyManager.SIM_STATE_PIN_REQUIRED): 
            System.out.println("*******************************************SIM_STATE_PIN_REQUIRED******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_PUK_REQUIRED): 
            System.out.println("*******************************************SIM_STATE_PUK_REQUIRED******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_UNKNOWN): 
            System.out.println("*******************************************SIM_STATE_UNKNOWN******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_READY): 
        {

        }
    break;
    }
    default: break;
    }
</code></pre>

<p>I'm doing this but dont 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 "SIM_STATE_UNKNOWN" and causes program's execution got complete. </p>

<p>Please help me to achieve this.</p>
