简体   繁体   中英

Windows Phone 8.1 emulator check Internet connection

I need to test my app in emulator, check Internet connection. I am using following class (from here http://www.jayway.com/2015/04/23/how-to-verify-internet-access-in-universal-apps/ ):

public class Connection
{
    public static bool HasInternetAccess { get; private set; }

    public Connection() {
        NetworkInformation.NetworkStatusChanged += NetworkInformationOnNetworkStatusChanged;
        CheckInternetAccess();
    }

    private void NetworkInformationOnNetworkStatusChanged(object sender) {
        CheckInternetAccess();
    }

    private void CheckInternetAccess() {
        var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        HasInternetAccess = (connectionProfile != null &&
                             connectionProfile.GetNetworkConnectivityLevel() ==
                             NetworkConnectivityLevel.InternetAccess);
    }
}

and checking connection by using class:

Connection conn = new Connection();
if (Connection.HasInternetAccess) {
      // some code
} else {
      MessageDialog msgbox = new MessageDialog("no internet");
      await msgbox.ShowAsync();
}

Problem is I get true every time, in emulator. I tried solution from here How to disconnect Windows Phone 8.1 emulator from network? , even rebooted PC, but nothing changed.

Testing on the device was correct. True if connection enabled, false if connection disabled.

PS Sorry for my terrible English.

The emulator always returns NetworkInformation.GetInternetConnectionProfile() as true, even if you emulate network conditions as having no network.

Use physical device to test the app.(it will works fine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM