在 Windows 10 UWP 应用中,我如何检测当前的网络连接是 Wifi 还是蜂窝数据?
var temp = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (temp.IsWlanConnectionProfile)
{
// its wireless
}else if (temp.IsWwanConnectionProfile)
{
// its mobile
}
我希望这可以帮到你。
除了获得连接(其他人提到的),您还可以更好地处理计量连接。
switch (connectionCost.NetworkCostType)
{
case NetworkCostType.Unrestricted:
//
break;
case NetworkCostType.Fixed:
//
break;
case NetworkCostType.Variable:
//
break;
case NetworkCostType.Unknown:
//
break;
default:
//
break;
}
请到GitHub查看有关网络演示的信息。
if (connectionCost.Roaming || connectionCost.OverDataLimit)
{
Cost = NetworkCost.OptIn;
Reason = connectionCost.Roaming
? "Connection is roaming; using the connection may result in additional charge."
: "Connection has exceeded the usage cap limit.";
}
else if (connectionCost.NetworkCostType == NetworkCostType.Fixed
|| connectionCost.NetworkCostType == NetworkCostType.Variable)
{
Cost = NetworkCost.Conservative;
Reason = connectionCost.NetworkCostType == NetworkCostType.Fixed
? "Connection has limited allowed usage."
: "Connection is charged based on usage. ";
}
else
{
Cost = NetworkCost.Normal;
Reason = connectionCost.NetworkCostType == NetworkCostType.Unknown
? "Connection is unknown"
: "Connection cost is unrestricted";
}
temp
是否为null
。 - Nick