.NET for AndroidでデバイスのSIMスロット数を取得する

必要な権限 編集

  • android.permission.READ_PHONE_STATE

TelephonyManagerを使うには権限が必要です。

大雑把なコード 編集

var telephonyManager = GetSystemService(TelephonyService) as TelephonyManager;
if (telephonyManager == null)
    throw new NotSupportedException();

// API レベルに応じて SIM スロット数を取得
int simSlotCount = 0;

if (Android.OS.BuildVersionCodes.R <= Android.OS.Build.VERSION.SdkInt)
{// API Level 30 (Android 11) 以上
    simSlotCount = telephonyManager.ActiveModemCount;
}
else if (Android.OS.BuildVersionCodes.M <= Android.OS.Build.VERSION.SdkInt)
{// API Level 23 (Android 6.0) 以上
    simSlotCount = telephonyManager.PhoneCount;
}
else
{// デュアルSIMなどという軟弱な概念はない
    throw new NotSupportedException();
}

Console.WriteLine($"SIM Slot count: {simSlotCount}");