Windows Phone 8.1: Check Internet Connection

给你一囗甜甜゛ 提交于 2020-01-01 10:52:28

问题


How can I know if the phone has internet connection? (Whether WiFi or Data)

Sometimes the phone is connecting to WiFi without internet connection like HotSpots. So I want a code to know if the phone is connecting to internet.


回答1:


You can simply try:

 if (NetworkInformation.GetInternetConnectionProfile() == null)
        {
            //no connection
        }

As you can see in this msdn documentation:NetworkInformation.GetInternetConnectionProfile

It will return null if there is "no connection profile with a suitable connection"

You can also check explicity the "Internet Access" level with this: NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess

I Think this will work also in universal app.




回答2:


The below Method works for me simply to check if the device is connected to internet or not even in universal windows app. After creating the connection class you can simply use it anywhere just by instantiating this class...

public class Connection
{
   public bool CheckInternetAccess()
   {
        var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        var HasInternetAccess = (connectionProfile != null &&
                             connectionProfile.GetNetworkConnectivityLevel() ==
                             NetworkConnectivityLevel.InternetAccess);
        return HasInternetAccess;
   }
}

To use this class simply..

 Connection objConnection = new Connection();
 if(objConnection.CheckInternetAccess()==true)
 {
   //todo
 }
 else
 {//todo}



回答3:


What you want is a captive portal, which is pretty much a page that users connect to, to test whether their internet connection is working, it can be explained here in greater detail.

These open source projects look promising:

  1. WiFiDog
  2. ChilliSpot

Good Luck!




回答4:


Please consider checking internet in background thread

if (await Task.Run(() =>NetworkInterface.GetIsNetworkAvailable())
{
   //Wifi or Cellular
}
else
{
   // No internet
}


来源:https://stackoverflow.com/questions/27647505/windows-phone-8-1-check-internet-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!