我在windows mobile模拟器中使用了以下功能来检查网络状态。它返回“未知”以太网是否存在。
function checkConnection() {
var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
checkConnection();
答案 0 :(得分:1)
在Windows Phone上,For get当前界面可以使用“NetworkInterfaceList”类:
NetworkInterfaceInfo CurrentInterface;
var interfacesList = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo specificInterface in interfacesList)
{
CurrentInterface = specificInterface;
break;
}
您可以订阅网络更改活动:
DeviceNetworkInformation.NetworkAvailabilityChanged += NetworkChanged;
private void NetworkChanged(object sender, NetworkNotificationEventArgs e)
{
CurrentInterface = e.NetworkInterface;
}
获取当前界面后,您可以获得真正的网络类型:
switch (CurrentInterface.InterfaceType)
{
case NetworkInterfaceType.Ethernet:
// ETHERNET (USB connected)
break;
case NetworkInterfaceType.MobileBroadbandCdma:
switch (pNetworkInterfaceInfo.InterfaceSubtype)
{
case NetworkInterfaceSubType.Cellular_GPRS:
// 2G
break;
case NetworkInterfaceSubType.Cellular_EDGE:
// 2GP
break;
case NetworkInterfaceSubType.Cellular_EVDO:
case NetworkInterfaceSubType.Cellular_EVDV:
// ?
break;
case NetworkInterfaceSubType.Cellular_1XRTT:
// ?
break;
case NetworkInterfaceSubType.Cellular_3G:
// 3G
break;
case NetworkInterfaceSubType.Cellular_HSPA:
// 3GP
break;
case NetworkInterfaceSubType.Unknown:
// Unknonwn ? 4G is Unknown.
break;
}
break;
case NetworkInterfaceType.MobileBroadbandGsm:
switch (pNetworkInterfaceInfo.InterfaceSubtype)
{
case NetworkInterfaceSubType.Cellular_GPRS:
// 2G
break;
case NetworkInterfaceSubType.Cellular_EDGE:
// 2GP
break;
case NetworkInterfaceSubType.Cellular_EVDO:
case NetworkInterfaceSubType.Cellular_EVDV:
// ??
break;
case NetworkInterfaceSubType.Cellular_1XRTT:
// ??
break;
case NetworkInterfaceSubType.Cellular_3G:
// 3G
break;
case NetworkInterfaceSubType.Cellular_HSPA:
// 3GP
break;
case NetworkInterfaceSubType.Unknown:
// Unknown? 4G is Unknown
break;
}
break;
case NetworkInterfaceType.Wireless80211:
// WIFI
break;
}
请注意,对于Windows Phone 7项目,我们没有“4G类型”,当我用设备测试4G时,NetworkInterfaceType是未知的......但是,如果我们在32G或3G中遇到网络问题,也是未知的..
我没有使用Windows Phone 8项目进行测试,但从理论上讲,它适用于4G。
此外,您可以在网络类型here上获得更多详细信息。
PS:为了测试网络,没有比使用真实设备测试更好的了,因此,真正的网络。