【问题标题】:Check internet Connection on phone在手机上检查互联网连接
【发布时间】:2014-08-04 11:04:44
【问题描述】:

我想检查我的手机是否可以连接到互联网。我已经看到了几个问题。其中之一是 Question 。它说要使用NetworkInterface.GetIsNetworkAvailable() 这个,我试过了。我已经断开了我的电脑与互联网的连接,还关闭了模​​拟器的 DataConnection,但 NetworkInterface.GetIsNetworkAvailable() 这总是返回 true。但同时我也检查了NetworkInterfaceType.None,有趣的是它即将为空。 谁能解释我在哪里缺少信息?

尝试:-

public static void CheckNetworkAvailability()
    {
       // this is coming true even when i disconnected my pc from internet.
       // i also make the dataconnection off of the emulator
        var fg = NetworkInterface.GetIsNetworkAvailable();

        var ni = NetworkInterface.NetworkInterfaceType;
        // this part is coming none  
        if (ni == NetworkInterfaceType.None)
            IsConnected = false;

    }

任何帮助表示赞赏:)

【问题讨论】:

  • 一般情况下您会使用 Internet,还是在检查是否可以连接到后端服务器?

标签: c# .net windows-phone-8 windows-phone-8.1 windows-phone-sl-8.1


【解决方案1】:

我正在使用以下代码检查设备是否可以访问互联网,以及它是否连接到 wifi 或数据连接..

 public void UpdateNetworkInformation()
    {
        // Get current Internet Connection Profile.
        ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

        //air plan mode is on...
        if (internetConnectionProfile == null)
        {
            Is_Connected = false;
            return;
        }

        //if true, internet is accessible.
        this.Is_InternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;

        // Check the connection details.
        else if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection. 
        {
            Is_Roaming = internetConnectionProfile.GetConnectionCost().Roaming;

            /// user is Low on Data package only send low data.
            Is_LowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit;

            //User is over limit do not send data
            Is_OverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit;

        }
        else //Connection is a Wi-Fi connection. Data restrictions are not necessary. 
        {
            Is_Wifi_Connected = true;
        }
    }

编辑: 对于简单的互联网连接,您可以使用以下代码。

  System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    模拟器始终将NetworkInterface.GetIsNetworkAvailable() 作为真返回,即使您将网络条件模拟为没有网络。

    我自己也遇到过这个问题,测试这种行为的唯一真正方法是将应用程序部署到运行 Windows Phone 的物理设备上,并在关闭数据的情况下对其进行测试。

    【讨论】:

    • 下面的答案对吗?我想检查我的应用程序是否可以连接到internet。没有别的。就像你说的你遇到了那么你如何解决它?
    • 在windows phone中,通常当设备没有连接到互联网时,它也没有连接到网络。所以我没有费心去更进一步。
    • 我还没有带设备,你通过案例测试了吗?
    【解决方案3】:

    NetworkInterface.GetIsNetworkAvailable() 检查network connection 而不是internet connection。如果您在任何类型的网络中,那么无论internet 是否存在,它都会返回true

    您可以通过以下方式检查互联网连接:

    using System.Net
    
    private bool IsOnline() 
    {
        try
        {
            IPHostEntry iPHostEntry = Dns.GetHostEntry("www.wikipedia.com");
            return true;
        }
        catch (SocketException ex) 
        {
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2011-04-15
      • 2017-03-16
      • 2014-02-08
      • 2012-06-12
      • 1970-01-01
      相关资源
      最近更新 更多