In Windows Phone Development, it is sometimes necessary to get the type of device that is currently connected to the network, whether it is WiFi, or 2g,3g, or an API (Microsoft.Phone.Net.NetworkInformation) that provides access to the network type in 4G,SDK. Devicenetworkinformation), we can easily access the relevant information through this API. But this API is obtained based on asynchronous callbacks, and if you need to get them synchronously, you need to do some special processing.
The following code defines the network type information class that is used to store the obtained network type information.
<summary>
///Network type
///</summary> public
enum Networktype
{
None = 0,
Wifi = 1, //wifi
mobile2g = 2, //2g network
mobile3g = 3, //3g network
mobile4g = 4 //4g network
}
///<summary>
///Network type information
///</summary> public
class Networkinfo
{
///<summary>
/// Network interface name
///</summary> public
string InterfaceName {get; set;}
<summary>
///A description of the network interface
///</summary> public
string Description {get; set;}
<summary>
///Network type
///</summary> public
networktype InterfaceType {get; set;}
}
The following code obtains the network type information from the system Networkinterfaceinfo parsing and writes to the Networkinfo class constructed above.
private static Networkinfo Parsenetworkinfo (Networkinterfaceinfo info) {Networkinfo netInfo = new Ne
Tworkinfo (); if (info!= null) {netinfo.interfacename = info.
InterfaceName; Netinfo.description = info.
Description; Switch (info. InterfaceType) {Case NETWORKINTERFACETYPE.MOBILEBROADBANDCDMA:CA Se NetworkInterfaceType.MobileBroadbandGsm:switch (info.
Interfacesubtype) {case NETWORKINTERFACESUBTYPE.CELLULAR_GPRS: Case NetworkInterfaceSubType.Cellular_1XRTT:case Networkinterfacesubtyp
E.cellular_edge:netinfo.interfacetype = networktype.mobile2g;
Break
Case NETWORKINTERFACESUBTYPE.CELLULAR_3G: Case NetworkInterfaceSubType.Cellular_EVDO:case NETWORKINTERFACESUBTYPE.CELLULAR_EVDV: Case NetworkInterfaceSubType.Cellular_HSPA:netInfo.InterfaceType
= NETWORKTYPE.MOBILE3G;
Break
Case NetworkInterfaceSubType.Cellular_LTE:case NETWORKINTERFACESUBTYPE.CELLULAR_EHRPD:
Netinfo.interfacetype = networktype.mobile4g;
Break
Default:netInfo.InterfaceType = Networktype.none;
Break
} break;
Case NetworkInterfaceType.Wireless80211:netInfo.InterfaceType = Networktype.wifi;
Break Default:netInfo.InterFacetype = Networktype.none;
Break
} return netInfo; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/