SIMカードがAndroidデバイスで利用可能であるかどうかを確認するにはどうすればよいですか?
-
09-10-2019 - |
質問
デバイスがプログラムでSIMカードを持っているかどうかを確認するのにヘルプが必要です。サンプルコードを提供してください。
解決
TelephonyManagerを使用します。
http://developer.android.com/reference/android/telephony/telephonymanager.html
ファルマリが指摘しているように、あなた 意思 使いたい GetPhoneType まず第一に、GSM電話を扱っているかどうかを確認します。もしそうなら、SIM状態を取得することもできます。
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;
}
編集:
API 26から始まる(Android Oプレビュー)使用して、個々のSIMスロットのSimStateを照会できます getSimState(int slotIndex)
つまり:
int simStateMain = telMgr.getSimState(0);
int simStateSecond = telMgr.getSimState(1);
APIを使用して開発している場合は、使用できます TelephonyManager's
String getDeviceId (int slotIndex)
//returns null if device ID is not available. ie. query slotIndex 1 in a single sim device
int devIdSecond = telMgr.getDeviceId(1);
//if(devIdSecond == null)
// no second sim slot available
API 23 -Docsに追加されました ここ
他のヒント
以下のコードで確認できます。
public static boolean isSimSupport(Context context)
{
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); //gets the current TelephonyManager
return !(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT);
}
これを行う別の方法を見つけました。
public static boolean isSimStateReadyorNotReady() {
int simSlotCount = sSlotCount;
String simStates = SystemProperties.get("gsm.sim.state", "");
if (simStates != null) {
String[] slotState = simStates.split(",");
int simSlot = 0;
while (simSlot < simSlotCount && slotState.length > simSlot) {
String simSlotState = slotState[simSlot];
Log.d("MultiSimUtils", "isSimStateReadyorNotReady() : simSlot = " + simSlot + ", simState = " + simSlotState);
if (simSlotState.equalsIgnoreCase("READY") || simSlotState.equalsIgnoreCase("NOT_READY")) {
return true;
}
simSlot++;
}
}
return false;
}
所属していません StackOverflow