如何检查WP8设备是否使用wifi,移动计划或漫游来加载数据

我计划只加载我的应用程序所需的数据。 这意味着,当通过Wifi加载数据时,我想预取东西。 如果数据是通过移动计划甚至漫游加载的,我想问一下用户。

但是,我只找到了Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation,它向我提供了有关可用内容的反馈,而不是实际使用的内容。 NetworkInterface.GetInternetInterface()也可以,但不会给我详细说明它是否正在漫游。

有什么办法吗?

让我们自己解决一下:

有一个Data Sense API ,它不仅可以检查设备是否在漫游,还可以检查应用程序是否接近或超过Data Sense中设置的数据限制。 当提供程序不允许使用Data Sense UI时,API也可以使用。

特别是,上面链接中的代码解决了一切!

// Get current Internet Connection Profile. ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); // Check the connection details. if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != IANA_INTERFACE_TYPE_WIFI) { // Connection is not a Wi-Fi connection. if (internetConnectionProfile.GetConnectionCost().Roaming) { // User is roaming. Don't send data out. m_bDoNotSendData = true; } if (internetConnectionProfile.GetConnectionCost().ApproachingDataLimit) { // User is approaching data limit. Send low-resolution images. m_bSendLowResolutionImage = true; } if (internetConnectionProfile.GetConnectionCost().OverDataLimit) { // User is over data limit. Don't send data out. m_bDoNotSendData = true; } } else { //Connection is a Wi-Fi connection. Data restrictions are not necessary. m_bDoNotSendData = false; m_bSendLowResolutionImage = false; } // Optionally, report the current values in a TextBox control. string cost = string.Empty; switch (internetConnectionProfile.GetConnectionCost().NetworkCostType) { case NetworkCostType.Unrestricted: cost += "Cost: Unrestricted"; break; case NetworkCostType.Fixed: cost += "Cost: Fixed"; break; case NetworkCostType.Variable: cost += "Cost: Variable"; break; case NetworkCostType.Unknown: cost += "Cost: Unknown"; break; default: cost += "Cost: Error"; break; } cost += "\n"; cost += "Roaming: " + internetConnectionProfile.GetConnectionCost().Roaming + "\n"; cost += "Over Data Limit: " + internetConnectionProfile.GetConnectionCost().OverDataLimit + "\n"; cost += "Approaching Data Limit : " + internetConnectionProfile.GetConnectionCost().ApproachingDataLimit + "\n"; NetworkStatus.Text = cost; 

您是否在WP8上尝试过System.Net.NetworkInformation命名空间。 该软件包具有静态方法,可返回网络状态。 然后,您可以根据此信息进行切换

NetworkInterface.NetworkInterfaceType

更多细节可以在这里找到

这款手机可以同时使用3G和WiFi。 我测试的情况 – 手机在发现并连接到WiFi网络后,没有关闭在3G上打开的现有TCP连接。 如果打开一个新的TCP连接,它将使用WiFi。

Microsoft.Phone.dll中有SocketExtensions.GetCurrentNetworkInterface扩展方法,它告诉您哪个接口用于指定的套接字。