大卫的回答很到位。我想添加一些示例代码来帮助人们开始实现这样的状态监视器。
/**
* Handles broadcasts related to SIM card state changes.
* <p>
* Possible states that are received here are:
* <p>
* Documented:
* ABSENT
* NETWORK_LOCKED
* PIN_REQUIRED
* PUK_REQUIRED
* READY
* UNKNOWN
* <p>
* Undocumented:
* NOT_READY (ICC interface is not ready, e.g. radio is off or powering on)
* CARD_IO_ERROR (three consecutive times there was a SIM IO error)
* IMSI (ICC IMSI is ready in property)
* LOADED (all ICC records, including IMSI, are loaded)
* <p>
* Note: some of these are not documented in
* https://developer.android.com/reference/android/telephony/TelephonyManager.html
* but they can be found deeper in the source code, namely in com.android.internal.telephony.IccCardConstants.
*/
public class SimStateChangedReceiver extends BroadcastReceiver {
/**
* This refers to com.android.internal.telehpony.IccCardConstants.INTENT_KEY_ICC_STATE.
* It seems not possible to refer it through a builtin class like TelephonyManager, so we
* define it here manually.
*/
private static final String EXTRA_SIM_STATE = "ss";
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getExtras().getString(EXTRA_SIM_STATE);
if (state == null) {
return;
}
// Do stuff depending on state
switch (state) {
case "ABSENT": break;
case "NETWORK_LOCKED": break;
// etc.
}
}
}