How to monitor SIM state change

Gianni Costanzi picture Gianni Costanzi · May 10, 2012 · Viewed 27.4k times · Source

I'd like to be able to do some stuff when the SIM state change, i.e. play a sound when SIM PIN is required, but I think there are no Broadcast events that can be intercepted by a broadcast receiver for this... registering for android.intent.action.PHONE_STATE does only tell you when the CALL-STATE changes.. An alternative can be starting a service that registers a PhoneStateListener and reacts upon a LISTEN_SERVICE_STATE (when the state is OUT-OF-STATE it can get the SIM state from the TelephonyManager and look if the state is SIM_STATE_PIN_REQUIRED). So, my questions are:

1) Is there any broadcast intent that I can use to intercept a SIM state change or a Service State change?

2) is it a bad idea to install a PhoneStateListener within a Service and use it to deliver intents to the Service itself upon the notification of a phone state changed received by the PhoneStateListener?

Answer

David Wasser picture David Wasser · May 11, 2012

The Intent android.intent.action.SIM_STATE_CHANGED is broadcast when the SIM state changes. For example, on my HTC Desire with a T-Mobile SIM card, if I put the device into flight mode the following Intent is broadcast:

  • Intent: android.intent.action.SIM_STATE_CHANGED with extras: ss = NOT_READY, reason = null

If I then take it out of flight mode, the following Intents are broadcast:

  • Intent: android.intent.action.SIM_STATE_CHANGED with extras: ss = LOCKED, reason = PIN
  • Intent: android.intent.action.SIM_STATE_CHANGED with extras: ss = READY, reason = null
  • Intent: android.intent.action.SIM_STATE_CHANGED with extras: ss = IMSI, reason = null
  • Intent: android.intent.action.SIM_STATE_CHANGED with extras: ss = LOADED, reason = null

It is possible that different manufacturers and different models behave differently. As they say, "Your mileage may vary".