gpt4 book ai didi

c# - Xamarin - 查看设备连接到 Wifi 网络的可靠方式 (Android)

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:14 25 4
gpt4 key购买 nike

我正在 Android 上构建一个移动应用程序,用户可以在可配置的设置中决定如果未连接到 Wifi 则不发送数据,如下所示:

enter image description here

我正在寻找一种可靠的方法来检查这一点并提出了这个解决方案:

public class AndroidDeviceNetwork : IDeviceNetwork
{
private const string UNKNOWNSSID = "<unknown ssid>";

/// <summary>
/// Detects if the device has wifi turned on (does not take into account if the
/// device is connected to a wifi network, only the fact its switched on).
/// </summary>
/// <returns>True if switched on only. False if not switched on.</returns>
public bool IsWifiEnabled()
{
var wifiManager = Application.Context.GetSystemService(Context.WifiService)
as WifiManager;

if (wifiManager != null)
{
// Check state is enabled.
return wifiManager.IsWifiEnabled;
}

return false;
}

/// <summary>
/// Checks wifi is switched on AND that its connected (using NetworkId and SSID to
/// identify connected).
/// </summary>
/// <returns>True if switched on and connected to a wifi network. False if not switch on
/// OR if switched on but not connected.</returns>
public bool IsWifiConnected()
{
var wifiManager = Application.Context.GetSystemService(Context.WifiService)
as WifiManager;

if (wifiManager != null)
{
// Check state is enabled.
return wifiManager.IsWifiEnabled &&
// Check for network id equal to -1
(wifiManager.ConnectionInfo.NetworkId != -1
// Check for SSID having default value of "<unknown SSID>"
&& wifiManager.ConnectionInfo.SSID != UNKNOWNSSID);
}
return false;
}
}

谁能验证这是否是检查用户是否连接到无线网络的好方法(IsWifiConnected() 方法)?或者有没有更靠谱的方法?

提前致谢!

最佳答案

仅供引用 - 这是我想出的类来管理这个(仅限 Android 的解决方案):

public class AndroidDeviceNetwork : IDeviceNetwork
{
private const string UNKNOWNSSID = "<unknown ssid>";

public NetworkState GetNetworkState()
{
var connMgr = Application.Context.GetSystemService(Context.ConnectivityService)
as ConnectivityManager;

if (connMgr == null)
return NetworkState.NoNetwork;

var activeNetwork = connMgr.ActiveNetworkInfo;

if (activeNetwork == null || !activeNetwork.IsConnectedOrConnecting)
return NetworkState.NoNetwork;

if (activeNetwork.Type == ConnectivityType.Wifi)
return NetworkState.WiFi;

if (activeNetwork.Type == ConnectivityType.Mobile)
return NetworkState.Mobile;

return NetworkState.NoNetwork;
}

/// <summary>
/// Detects if the device has wifi turned on (does not take into account if the
/// device is connected to a wifi network, only the fact its switched on).
/// </summary>
/// <returns>True if switched on only. False if not switched on.</returns>
public bool IsWifiEnabled()
{
var wifiManager = Application.Context.GetSystemService(Context.WifiService)
as WifiManager;

// Check state is enabled.
if (wifiManager != null)
return wifiManager.IsWifiEnabled;

return false;
}

/// <summary>
/// Detects if the device has MobileData turned on
/// </summary>
/// <returns>True if switched on only. False if not switched on.</returns>
public bool IsMobileDataEnabled()
{
var connectivityManager = (ConnectivityManager)Application.Context.GetSystemService(Context.ConnectivityService);
var networkInfo = connectivityManager?.ActiveNetworkInfo;
return networkInfo?.Type == ConnectivityType.Mobile;
}

/// <summary>
/// Checks wifi is switched on AND that its connected (using NetworkId and SSID to
/// identify connected).
/// </summary>
/// <returns>True if switched on and connected to a wifi network. False if not switch on
/// OR if switched on but not connected.</returns>
public bool IsWifiConnected()
{
var wifiManager = Application.Context.GetSystemService(Context.WifiService) as WifiManager;

if (wifiManager != null)
{
// Check state is enabled.
return wifiManager.IsWifiEnabled &&
// Check for network id equal to -1
(wifiManager.ConnectionInfo.NetworkId != -1
// Check for SSID having default value of "<unknown SSID>"
&& wifiManager.ConnectionInfo.SSID != UNKNOWNSSID);
}

return false;
}
}

我实现了 IDeviceNetwork 接口(interface),因此可以将其注入(inject)到跨平台工作的通用库中。简单的界面如下所示:

public interface IDeviceNetwork
{
bool IsWifiEnabled();
bool IsWifiConnected();
NetworkState GetNetworkState();
}

希望这对其他人有用!

关于c# - Xamarin - 查看设备连接到 Wifi 网络的可靠方式 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com