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

提供:MonoBook
2024年10月22日 (火) 02:36時点におけるAdministrator (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

必要な権限[編集 | ソースを編集]

  • 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}");