Android listens to SIM status
- /*
- Listen to the broadcasting of sim status changes, and return the sim card status, valid or invalid.
- If either card is in valid status, the system returns valid status. If both cards are invalid, the system returns invalid status.
- */
- Import android. app. Service;
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. Intent;
- Import android. telephony. TelephonyManager;
-
- Public class SimStateReceive extends BroadcastReceiver {
- Private final static String ACTION_SIM_STATE_CHANGED = "android. intent. action. SIM_STATE_CHANGED ";
- Private final static int SIM_VALID = 0;
- Private final static int SIM_INVALID = 1;
- Private int simState = SIM_INVALID;
-
- Public int getSimState (){
- Return simState;
- }
-
- @ Override
- Public void onReceive (Context context, Intent intent ){
- If (intent. getAction (). equals (ACTION_SIM_STATE_CHANGED )){
- TelephonyManager tm = (TelephonyManager) context. getSystemService (Service. TELEPHONY_SERVICE );
- Int state = tm. getSimState ();
- Switch (state ){
- Case TelephonyManager. SIM_STATE_READY:
- SimState = SIM_VALID;
- Break;
- Case TelephonyManager. SIM_STATE_UNKNOWN:
- Case TelephonyManager. SIM_STATE_ABSENT:
- Case TelephonyManager. SIM_STATE_PIN_REQUIRED:
- Case TelephonyManager. SIM_STATE_PUK_REQUIRED:
- Case TelephonyManager. SIM_STATE_NETWORK_LOCKED:
- Default:
- SimState = SIM_INVALID;
- Break;
- }
- }
- }
-
- }