Windows Phone 8.1:检查互联网连接

5

如何知道手机是否连接了互联网?(无论是WiFi还是数据)

有时手机会连接到没有互联网连接的WiFi热点。因此,我想要一段代码来判断手机是否连接到了互联网。


这个链接 https://stackoverflow.com/questions/22362646/windows-phone-8-how-to-check-network-availability 有帮助吗? - matthew5025
你需要它做什么?网络服务? - sertsedat
1
在调用 Web 服务之前,我想知道用户是否连接到互联网... - Vahid
4个回答

2
你可以尝试以下方法:
 if (NetworkInformation.GetInternetConnectionProfile() == null)
        {
            //no connection
        }

您可以在此MSDN文档中看到:NetworkInformation.GetInternetConnectionProfile 如果没有“适当的连接配置文件”,它将返回null
您还可以使用以下内容明确检查“互联网访问”级别:NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess 我认为这也适用于通用应用程序。

1
以下方法对我来说非常有效,可以简单地检查设备是否连接到互联网,即使在通用Windows应用程序中也是如此。创建连接类后,您只需通过实例化此类即可在任何地方使用它...
public class Connection
{
   public bool CheckInternetAccess()
   {
        var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        var HasInternetAccess = (connectionProfile != null &&
                             connectionProfile.GetNetworkConnectivityLevel() ==
                             NetworkConnectivityLevel.InternetAccess);
        return HasInternetAccess;
   }
}

使用这个类很简单,只需...
 Connection objConnection = new Connection();
 if(objConnection.CheckInternetAccess()==true)
 {
   //todo
 }
 else
 {//todo}

0
请考虑在后台线程中检查互联网。
if (await Task.Run(() =>NetworkInterface.GetIsNetworkAvailable())
{
   //Wifi or Cellular
}
else
{
   // No internet
}

0
你需要的是一个被称为“Captive Portal”的页面,用户可以通过连接到该页面来测试他们的互联网连接是否正常工作。你可以在这里找到更详细的解释。
以下这些开源项目看起来很有前途:
  1. WiFiDog
  2. ChilliSpot
祝你好运!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接