Skip to content

Instantly share code, notes, and snippets.

@ChiiAyano
Created November 4, 2024 04:42
Show Gist options
  • Save ChiiAyano/d375947207409465b10b3393cf3ff838 to your computer and use it in GitHub Desktop.
Save ChiiAyano/d375947207409465b10b3393cf3ff838 to your computer and use it in GitHub Desktop.
using Android.App;
using Android.Content.PM;
namespace MauiApp3;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
private NetworkStrengthWatcher? networkStrengthWatcher;
protected override void OnStart()
{
base.OnStart();
this.networkStrengthWatcher ??= new NetworkStrengthWatcher(this);
// サクッとスタート
this.networkStrengthWatcher.Listen();
}
protected override void OnDestroy()
{
// ここで止める
this.networkStrengthWatcher?.Stop();
base.OnDestroy();
}
}
using Android.Content;
using Android.Telephony;
namespace MauiApp3;
public class NetworkStrengthWatcher
{
/// <summary>
/// 携帯ネットワーク通信に関するコールバックをまるめるインターフェース
/// </summary>
private interface ITelephonyState
{
/// <summary>
/// 電波強度
/// </summary>
int Level { get; }
}
/// <summary>
/// Android API Level 31 以降用
/// </summary>
private class TelephonyState :
TelephonyCallback,
TelephonyCallback.ISignalStrengthsListener,
ITelephonyState
{
public int Level { get; private set; }
public void OnSignalStrengthsChanged(SignalStrength signalStrength)
{
// 電波強度を見やすいように 0 から 4 にしてくれているプロパティを使う
this.Level = signalStrength.Level;
Console.WriteLine($"Level: {this.Level}");
}
}
/// <summary>
/// Android API Level 30 以前用
/// </summary>
private class TelephonyStateOlder : PhoneStateListener, ITelephonyState
{
public int Level { get; private set; }
public override void OnSignalStrengthsChanged(SignalStrength? signalStrength)
{
if (OperatingSystem.IsAndroidVersionAtLeast(31))
{
return;
}
base.OnSignalStrengthsChanged(signalStrength);
if (signalStrength is null)
{
return;
}
// 電波強度を見やすいように 0 から 4 にしてくれているプロパティを使う
this.Level = signalStrength.Level;
Console.WriteLine($"Level: {this.Level}");
}
}
private MainActivity activity;
private TelephonyManager? telephonyManager;
private ITelephonyState telephonyState;
public NetworkStrengthWatcher(MainActivity mainActivity)
{
this.activity = mainActivity;
this.telephonyManager = mainActivity.GetSystemService(Context.TelephonyService) as TelephonyManager;
if (OperatingSystem.IsAndroidVersionAtLeast(31))
{
this.telephonyState = new TelephonyState();
}
else
{
this.telephonyState = new TelephonyStateOlder();
}
}
public void Listen()
{
if (OperatingSystem.IsAndroidVersionAtLeast(31))
{
// 本当は MainExecutor プロパティは nullable なので見た方がいいかも。ここでは知ったこっちゃない扱いにしておきます。
this.telephonyManager?.RegisterTelephonyCallback(this.activity.MainExecutor!, (TelephonyState)this.telephonyState);
}
else
{
this.telephonyManager?.Listen((TelephonyStateOlder)this.telephonyState, PhoneStateListenerFlags.SignalStrengths);
}
}
public void Stop()
{
if (OperatingSystem.IsAndroidVersionAtLeast(31))
{
// 素直なメソッドがあるのでそれを使えば止まる
this.telephonyManager?.UnregisterTelephonyCallback((TelephonyState)this.telephonyState);
}
else
{
// None を入れてリッスンすると止まる
this.telephonyManager?.Listen((TelephonyStateOlder)this.telephonyState, PhoneStateListenerFlags.None);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment