AndroidでSIMカードが刺さっているかどうかを確認したかったので、やり方を調べてみました。
以下のコードでSIMカードの状態が取得できます。
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telephonyManager.getSimState();
getSimState()
は以下のいずれかの値を返します。
- TelephonyManager.SIM_STATE_UNKNOWN
- TelephonyManager.SIM_STATE_ABSENT
- TelephonyManager.SIM_STATE_PIN_REQUIRED
- TelephonyManager.SIM_STATE_PUK_REQUIRED
- TelephonyManager.SIM_STATE_NETWORK_LOCKED
- TelephonyManager.SIM_STATE_READY
- TelephonyManager.SIM_STATE_NOT_READY
- TelephonyManager.SIM_STATE_PERM_DISABLED
- TelephonyManager.SIM_STATE_CARD_IO_ERROR
- TelephonyManager.SIM_STATE_CARD_RESTRICTED
いろいろな値がありますが、シンプルにSIMカードが刺さっていて、それが利用できるかどうかを判別したいだけならSTATE_READY
であるかどうかを判別すればOKです。
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telephonyManager.getSimState();
if (simState == TelephonyManager.SIM_STATE_READY) {
// SIMカードが刺さっていて利用可能な状態
}