![](/img/trans.png)
[英]How can I check if a Windows 8 device is currently sending/receiving data on an wifi/mobile data connection?
[英]How do I check whether a WP8 device uses wifi, mobile plan or roaming to load data
我计划仅加载应用程序所需的数据量。 这意味着,当通过Wifi加载数据时,我想预取东西。 如果数据是通过移动方案甚至漫游加载的,我想问用户。
但是,我只找到了Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation,它为我提供了有关可用内容而不是实际使用内容的反馈。 NetworkInterface.GetInternetInterface()也可以使用,但是没有提供有关是否漫游的详细信息。
有什么办法吗?
让我们自己解决:
有Data Sense API ,该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
扩展方法,该扩展方法告诉您哪个接口用于指定的套接字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.