Geo*_*kos 7 c# windows-phone-8 windows-phone-8.1
我目前正在将我的一个库移植到Windows Phone 8.1 Runtime并进入一个缺少的API,您可以在Windows Phone 8.0和Windows Phone Silverlight 8.1应用程序中使用它.
我需要的是DeviceNetworkInformation来获取连接到互联网的设备是什么类型的NetworkInterfaceType.
Windows Phone 8.0中的示例代码.
public void GetDeviceConnectionInfo()
{
DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint("microsoft.com", 80),
nrr =>
{
NetworkInterfaceInfo info = nrr.NetworkInterface;
if (info != null)
{
switch (info.InterfaceType)
{
case NetworkInterfaceType.Ethernet:
// Do something
break;
case NetworkInterfaceType.MobileBroadbandCdma:
case NetworkInterfaceType.MobileBroadbandGsm:
switch (info.InterfaceSubtype)
{
case NetworkInterfaceSubType.Cellular_3G:
case NetworkInterfaceSubType.Cellular_EVDO:
case NetworkInterfaceSubType.Cellular_EVDV:
case NetworkInterfaceSubType.Cellular_HSPA:
// Do something
break;
}
// Do something
break;
case NetworkInterfaceType.Wireless80211:
// Do something
break;
}
}
}, null);
}
Run Code Online (Sandbox Code Playgroud)
您可以访问运营商的名称DeviceNetworkInformation.CellularMobileOperator.
编辑:以下建议适用于Windows Phone 8.1应用程序.
我不推荐使用IanaInterfaceType,InboundMaxBitsPerSecond或OutboundMaxBitsPerSecond以确定连接类型中,他们是非常不准确的.
以下方法获取WP中状态栏中显示的连接类型.请注意,连接模式不一定表示上传/下载速度!
using Windows.Networking.Connectivity;
/// <summary>
/// Detect the current connection type
/// </summary>
/// <returns>
/// 2 for 2G, 3 for 3G, 4 for 4G
/// 100 for WiFi
/// 0 for unknown or not connected</returns>
private static byte GetConnectionGeneration()
{
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
if (profile.IsWwanConnectionProfile)
{
WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
switch (connectionClass)
{
//2G-equivalent
case WwanDataClass.Edge:
case WwanDataClass.Gprs:
return 2;
//3G-equivalent
case WwanDataClass.Cdma1xEvdo:
case WwanDataClass.Cdma1xEvdoRevA:
case WwanDataClass.Cdma1xEvdoRevB:
case WwanDataClass.Cdma1xEvdv:
case WwanDataClass.Cdma1xRtt:
case WwanDataClass.Cdma3xRtt:
case WwanDataClass.CdmaUmb:
case WwanDataClass.Umts:
case WwanDataClass.Hsdpa:
case WwanDataClass.Hsupa:
return 3;
//4G-equivalent
case WwanDataClass.LteAdvanced:
return 4;
//not connected
case WwanDataClass.None:
return 0;
//unknown
case WwanDataClass.Custom:
default:
return 0;
}
}
else if (profile.IsWlanConnectionProfile)
{
return 100;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
抱歉,我不知道运营商的名称,但接入点名称(APN)也可能有用,因为接入点已连接到运营商:
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
string apn = profile.WwanConnectionProfileDetails.AccessPointName;
Run Code Online (Sandbox Code Playgroud)
虽然并不完全相同,但我们可以在 Windows 8.1 中执行与早期版本中几乎相同的操作来检测网络类型,只是命名空间和类不同。可以在这里找到一篇涵盖大多数可能场景的好文章。
实际上,您无需访问有关网络连接的详细信息,而是根据NetworkCostType调整您的行为。基本上,如果用户的连接几乎没有计量,您就可以做您喜欢的事情,但如果他们使用数据计划或在拨打互联网会向用户收取费用的地方,您应该提示他们或者以不同的方式处理它。也许要等到 Wifi 或以太网可用。