プログラムでデバイスに SIM カードがあるかどうかを確認するのに助けが必要です。サンプルコードを提供してください。
53956 次
4 に答える
128
TelephonyManager を使用します。
http://developer.android.com/reference/android/telephony/TelephonyManager.html
Falmarri が指摘しているように、GSM 電話を扱っているかどうかを確認するには、getPhoneType FIRSTを使用する必要があります。そうであれば、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 Preview ) 以降では、次を使用して個々の 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 here
于 2010-10-20T22:13:19.907 に答える
15
以下のコードで確認できます。
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);
}
于 2015-08-10T13:21:20.640 に答える