「.NET for AndroidでデバイスのSIMスロット数を取得する」の版間の差分
Administrator (トーク | 投稿記録) ページの作成:「== 必要な権限 == * android.permission.READ_PHONE_STATE == 大雑把なコード == <source lang=csharp> 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 = tel…」 |
Administrator (トーク | 投稿記録) |
||
| 1行目: | 1行目: | ||
== 必要な権限 == | == 必要な権限 == | ||
* android.permission.READ_PHONE_STATE | * android.permission.READ_PHONE_STATE | ||
TelephonyManagerを使うには権限が必要です。 | |||
== 大雑把なコード == | == 大雑把なコード == | ||
2024年10月22日 (火) 02:35時点における版
必要な権限
- 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}");