Windows Phone Sync Way to get network type

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.