在android中检测sim卡是否准备就绪

时间:2016-02-13 05:30:54

标签: android

我使用以下代码片段来检查SIM卡是否准备就绪。代码工作正常,但即使启用了飞行模式,它也会返回“SIM_STATE_READY”。

private void detectSimState() {
    TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    int simState = telMgr.getSimState();
    switch (simState) {
    case TelephonyManager.SIM_STATE_ABSENT:
        Toast toast = Toast.makeText(this, "SIM_STATE_ABSENT",
                Toast.LENGTH_LONG);
        toast.show();
        // do something
        break;
    case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
        Toast toast1 = Toast.makeText(this, "SIM_STATE_NETWORK_LOCKED",
                Toast.LENGTH_LONG);
        toast1.show();
        // do something
        break;
    case TelephonyManager.SIM_STATE_PIN_REQUIRED:
        Toast toast11 = Toast.makeText(this, "SIM_STATE_PIN_REQUIRED",
                Toast.LENGTH_LONG);
        toast11.show();
        // do something
        break;
    case TelephonyManager.SIM_STATE_PUK_REQUIRED:
        Toast toast111 = Toast.makeText(this, "SIM_STATE_PUK_REQUIRED",
                Toast.LENGTH_LONG);
        toast111.show();
        // do something
        break;
    case TelephonyManager.SIM_STATE_READY:
        Toast toast1111 = Toast.makeText(this, "SIM_STATE_READY",
                Toast.LENGTH_LONG);
        toast1111.show();
        // do something
        break;
    case TelephonyManager.SIM_STATE_UNKNOWN:
        Toast toast2 = Toast.makeText(this, "SIM_STATE_UNKNOWN",
                Toast.LENGTH_LONG);
        toast2.show();
        // do something
        break;
    }
}

所以我该怎么解决这个问题?我相信有两种可能的解决方案 -

  1. 检查飞机模式是否开启
  2. 这可能无法在每个Android版本上运行

    1. 获取SIM卡无线电状态
    2. 我该怎么做?

      另外,在APi 17中加入了cellSignalStrength,所以这绝对不是我的一杯茶

1 个答案:

答案 0 :(得分:0)

我不明白为什么飞机模式检查会出问题。做这样的事情:

if(SIM_STATE_READY){

if(isAirplaneModeOn()){

//Sim is ready, but will give error. Handle exception here.

}

}

简单地这样实施。这有两种方法可以做到:

  1. 使用Settings.System类:

    private static boolean isAirplaneModeOn(Context context) {
       return Settings.System.getInt(context.getContentResolver(),
         Settings.System.AIRPLANE_MODE_ON, 0) != 0;
    
       }
    
  2. 使用BroadCast接收器:

  3. <强>清单:

    <receiver android:enabled="true" android:name=".ConnectivityReceiver">
        <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE"/>
        </intent-filter>
    </receiver>
    

    如果这有帮助,请告诉我。