作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在 Android 上构建一个移动应用程序,用户可以在可配置的设置中决定如果未连接到 Wifi 则不发送数据,如下所示:
我正在寻找一种可靠的方法来检查这一点并提出了这个解决方案:
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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!