Zennã«ãæ¸ãã¾ãã
ã»ã¼åãå
容ããããæè¡ã«ç¹åããããã°ã®Zennã«ãæ¸ãã¾ãã
zenn.dev
ã¯ããã«
æå ã§è©¦ããã®ãAndroidãããªãã®ã§ãAndroidã®ã¿ã¨ãªãã¾ããã.NET MAUIã§æºå¸¯ãããã¯ã¼ã¯éä¿¡ãWiFiæ¥ç¶ã®é»æ³¢å¼·åº¦ãåå¾ãã¦è¡¨ç¤ºããã¨ãããã¨ããã£ã¦ã¿ã¾ããMAUIã¨ãã¦ã¯ç¾å¨ã®ãããã¯ã¼ã¯æ¥ç¶ãä½ãã¯ãããã¾ãããé»æ³¢å¼·åº¦ãªã©ã¾ã§ã¯ãããã¾ãããããã§ãã©ãããã©ã¼ã åºæã®APIã使ç¨ãã¦åå¾ãããã¨ã試ã¿ã¾ãã
ãã©ãããã©ã¼ã åºæã®APIã使ç¨ããã®ã§ãä½æããcsãã¡ã¤ã«ã¯ãã¹ã¦ãPlatform/Androidãé ä¸ã«ç½®ãã¾ããã¾ããä½è¨ãªèæ ®ãããããªãã®ã§ããã®èª¬æä¸ã®ãæå°ã¿ã¼ã²ããAndroidãã¬ã¼ã ã¯ã¼ã¯ãã¯ãAndroid 10.0 (APIã¬ãã«29)ãã«ãã¦ããã¾ãã
æºå¸¯ãããã¯ã¼ã¯éä¿¡ã®é»æ³¢å¼·åº¦ãåå¾ãã
Android API Level 31 以éã¨ããããåã§å¿
è¦ãªç©ãå¤ããã¾ããæä½ãµãã¼ããã¼ã¸ã§ã³ã Android 12ã«ããå ´åã¯æ°ã«ããªãã¦ããã§ãããããããåããµãã¼ãããå ´åã¯ããããã«èæ
®ããå¿
è¦ãããã¾ãã
ã¤ã¾ããAndroid 12ããåã®ãã¼ã¸ã§ã³ã§ã¯ããAndroid.Telephonyãåå空éã®ãTelephonyManagerãã¯ã©ã¹(Android)ãListen()ãã¡ã½ãã(Android) ã使ç¨ãã¾ããã12以éã®ãã¼ã¸ã§ã³ã§ã¯éæ¨å¥¨ã¨ãªãããAndroid.Telephony.TelephonyManager.RegisterTelephonyCallback()ãã¡ã½ãã(Android) ã使ãã¾ãã
ããã¦ãåè
ãListen()ãã¡ã½ããã§å¿
è¦ãªã³ã¼ã«ããã¯ãåããã¯ã©ã¹ã¯ãAndroid.Telephony.PhoneStateListenerãã¯ã©ã¹(Android) ãç¶æ¿ããå¾è
ãRegisterTelephonyCallback()ãã¯ãAndroid.Telephony.TelephonyCallbackãã¯ã©ã¹(Android) ã®ç¶æ¿ãããã³ãAndroid.Telephony.TelephonyCallback.ISignalStrengthsListenerãã¤ã³ã¿ã¼ãã§ã¼ã¹(Android) ã®å®è£
ããããã®ãç¨æãã¾ããããã§ã両æ¹ãå¿
è¦ã«ãªãå ´åãç¹ã«å¾è
ã§å¿
è¦ãªã¯ã©ã¹ã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯API Level 31ãã追å ã«ãªã£ããã®ã§ã30以åã®OSãAndroid 11以åã§ã¯åå¨ããªãããå¤æ°ã¨ãã¦å®£è¨ã§ãã¾ããã®ã§ã両æ¹ã«å¯¾å¿ã§ãããããå¥ã§ã¤ã³ã¿ã¼ãã§ã¼ã¹ãªã©ãç¨æããããåã¨ãã¦å¤æ°å®£è¨ããæ¹æ³ãæ¡ãã¨ããããããã¾ããã
ã¡ãªã¿ã«ããã®æ å ±ãåå¾ããã®ã«å¿ è¦ãªæ¨©éã¯ããã¾ãããé©å½ã«æ¨©éããªããã¦ã¨ãã¥ã¬ã¼ã¿ã¼ãå®æ©ã«æµãã¦å®è¡ãã¦ã¿ã¾ããããç¹ã«æããããã¨ã¯ãªãã£ãã§ãã
using Android.Content; using Android.Telephony; 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; } } /// <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; } } }
ãã¨ã¯ãå¤æ°å®£è¨ã®åã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ãå®éã®ä»£å ¥ã«ã¯API Levelã«å¿ãããã®ãå ¥ãè¾¼ãã°OKã§ãã
private ITelephonyState telephonyState; public NetworkStrengthWatcher() { if (OperatingSystem.IsAndroidVersionAtLeast(31)) { this.telephonyState = new TelephonyState(); } else { this.telephonyState = new TelephonyStateOlder(); } }
ã¤ã¥ãã¦ãç¶æ ã®ã³ã¼ã«ããã¯ãåãåãããã®è³¼èªå¦çããããªãã¾ããæºå¸¯ãããã¯ã¼ã¯éä¿¡ã«ããããæ å ±ã¯ãAndroid.Telephony.TelephonyManagerãã¯ã©ã¹ã使ç¨ãã¾ããããã¯ãMainActivityãã®ã¤ã³ã¹ã¿ã³ã¹ããåå¾ã§ããã®ã§ãä»åä½æããã¯ã©ã¹ã«ãã®ã¤ã³ã¹ã¿ã³ã¹ã渡ãã¦ããããå¼ã³åºãã¾ãã
private MainActivity activity; private TelephonyManager? telephonyManager; private ITelephonyState telephonyState; public NetworkStrengthWatcher(MainActivity mainActivity) { this.activity = mainActivity; // ããã§åããã¯ããGetSystemService ã®è¿ãå¤ã nullable ãªã®ã§ as ã使ãã // åãåãã nullable ã«ãã¦ããã¾ã this.telephonyManager = mainActivity.GetSystemService(Context.TelephonyService) as TelephonyManager; if (OperatingSystem.IsAndroidVersionAtLeast(31)) { this.telephonyState = new TelephonyState(); } else { this.telephonyState = new TelephonyStateOlder(); } }
次ã«è³¼èªéå§ã®ã¡ã½ãããç¨æãã¾ããåè¿°ã®éããAPI Versionã«ãã£ã¦å¼ã³åºãã¡ã½ããã¯å¤ãããã¾ãå¿ è¦ãªã³ã¼ã«ããã¯ã¯ã©ã¹ãå¤ããã¾ãã®ã§ããã¼ã¸ã§ã³ã§åå²ããã¤ã¤å¼ã³åºãã¾ããã³ã¼ã«ããã¯ã¯ã©ã¹ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ã©ã¡ããå ¥ã£ã¦ãããããã«ãã¦ããã¾ããã®ã§ãããã§æ¬æ¥å¿ è¦ã«ãªãå®è£ åã«å¤æãã¦ãããã°OKã§ãã
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); } }
念ã®ããè³¼èªè§£é¤ãç¨æãã¦ããã¾ãããããã¡ããAPI Versionã«ãã£ã¦å¼ã³åºãã¡ã½ãããå¤ããã¾ãã31以éã®å ´åã¯ãUnregisterTelephonyCallback()ãã¡ã½ãããå¼ã³åºãã¨æ¢ã¾ãã¾ãããããããåã®å ´åã¯æ確ãªåæ¢ã¡ã½ãããããã¾ããã®ã§ããListen()ãã¡ã½ããã®ãã©ã°ã«ãPhoneStateListenerFlags.Noneãã渡ããã¨ã«ãªãã¾ãã
public void Stop() { if (OperatingSystem.IsAndroidVersionAtLeast(31)) { // ç´ ç´ãªã¡ã½ãããããã®ã§ããã使ãã°æ¢ã¾ã this.telephonyManager?.UnregisterTelephonyCallback((TelephonyState)this.telephonyState); } else { // None ãå ¥ãã¦ãªãã¹ã³ããã¨æ¢ã¾ã this.telephonyManager?.Listen((TelephonyStateOlder)this.telephonyState, PhoneStateListenerFlags.None); } }
ããã§æºåã¯OKã§ãããã¨ã¯å®éã«ä½¿ãã¨ããã§ããããã£ããããã§ã¯MainActivityã¯ã©ã¹å ã§å®£è¨ãããªãã¹ã³éå§ãããããã«ãã¾ãã
// ããã¸ã§ã¯ããä½ã£ãéã®ããããæ¸ãã¦ããã¨ããã¯çç¥ãã¦ããã¾ãã 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(); } }
ãããã¨ã¯å®è¡ããã°æ
å ±ãããã¦ãã¾ãããã°ããªããåºãã¦ãªãã®ã§ãã³ã¼ã«ããã¯ã¯ã©ã¹ã®ãOnSignalStrengthsChanged()ãã¡ã½ããå
ã§ãªãã¨ãªããã°ãåºãããã«ãã¦ããã°ããã¨æãã¾ãã
ã¨ãã¥ã¬ã¼ã¿ã¼ã§ãããã¯ã¼ã¯ã®ç¶æ
ãããããã¨ããã£ã¦ãããã°ããã°ã«ããã®æ
å ±ãéä¸å
¥ã£ã¦ãã¦ããã®ããããã¾ãã
ãã£ããããã¾ã§ã¾ã¨ã
é·ããªã£ãã®ã§ãæºå¸¯ãããã¯ã¼ã¯éä¿¡ã®åã§ä¸æ¦ç· ãã¾ããããã¾ã§ã§æ¸ããã³ã¼ãã¯ä»¥ä¸ã®gistã«é©å½ã«è²¼ã£ã¦ããã¾ããã
次ã¯WiFiã®æ
å ±ã«ã¤ãã¦æ¸ãã¾ãã
gist.github.com