簡體   English   中英

檢測sim卡是否已在android中准備好

[英]detect if sim card is ready in 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. 檢查飛行模式是否開啟/關閉

這可能無法在每個android版本上工作

  1. 取得SIM電台狀態

我怎么做?

此外,在APi 17中添加了cellSignalStrength ,所以絕對不是我喝杯茶

我不明白為什么飛行模式檢查會成為問題。 做這樣的事情:

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接收器:

表現:

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

讓我知道是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM