How to Monitor Sim State Change

How to monitor SIM state change

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".

Android: Use TelephonyManager or AudioManager for checking phone state before executing code

I don't think there's a best way because there is only one way, check the call state via TelephonyManager. No need for a receiver:

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
int callState = tm.getCallState();

See this for the call state constants.

Listening/Acting on Call State changes - service? activity? receiver?

I talked to someone and did a bit more research, and a broadcast receiver was indeed what I need. It's remarkably simple, actually.

Mostly followed the tutorial at http://www.vogella.de/articles/AndroidServices/article.html#receiver

Used How to edit shared preferences in an activity other than the one it is created in? when figuring out how to access preferences.



Related Topics



Leave a reply



Submit