Android 监听 SIM 卡状态

Android BroadcastReceiver About 2,607 words

判断是否包含 SIM 卡

/**
 * 判断是否包含SIM卡
 *
 * @return 状态
 */
public static boolean hasSimCard(Context context) {
    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; // 没有SIM卡
            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:
        // 无SIM卡
        break;
    case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
        // 锁定状态,需要网络的PIN码解锁
        break;
    case TelephonyManager.SIM_STATE_PIN_REQUIRED:
        // 锁定状态,需要用户的PIN码解锁
        break;
    case TelephonyManager.SIM_STATE_PUK_REQUIRED:
        // 锁定状态,需要用户的PUK码解锁
        break;
    case TelephonyManager.SIM_STATE_READY:
        // 有SIM卡
        break;
    case TelephonyManager.SIM_STATE_UNKNOWN:
        // 未知状态
        break;
}

监听 SIM 卡状态改变的广播

返回SIM卡的状态,有效或者无效。

双卡中只要有一张卡的状态有效即返回状态为有效,两张卡都无效则返回无效。

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) {  
        System.out.println("sim state changed");  
        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;  
            }  
        }  
    }  
}  
Views: 6,808 · Posted: 2019-04-09

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh