Android - 判断SIM卡状态

时间:2021-08-25 14:49:59

Android判断SIM卡状态, 是否插入SIM卡.
例如: 根据SIM判断优先使用的网络类型.

SIM卡状态

    /**
* 判断是否包含SIM卡
*
* @return 状态
*/

public static boolean hasSimCard() {
Context context = App.getAppContext();
TelephonyManager telMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
boolean result = true;
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
result = false; // 没有SIM卡
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
result = false;
break;
}
Log.d(TAG, result ? "有SIM卡" : "无SIM卡");
return result;
}

全部状态

        TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
// do something
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
// do something
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
// do something
break;
case TelephonyManager.SIM_STATE_READY:
// do something
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
// do something
break;
}