Windows Phone 7 monitors network environment changes

Source: Internet
Author: User

Some applications are sensitive to the network environment. WiFi is fast and free, 3G traffic is adopted, and GSM is slow. Therefore, we need to know how to determine the current network type, and how to be notified when the network type changes.

Msdn ProblemsArticle: How to: Detect Network Changes

The following is a brief introduction.

 

When the network changes, the network address changes. To monitor this event, you must useSystem. net. networkinformationThe networkchange class in the namespace.

To view the network connection type and status, all the classes used are inMicrosoft. Phone. net. networkinformationIn this namespace.

 

Note that the above two namespaces are different,Microsoft. Phone. net. networkinformationIs used for mobile phones,System. net. networkinformationUsed for various occasions. Because the names of these two classes conflict a lot, you must differentiate them when using them together.

 

Notified of network environment changes

You only need to listen to one event:

Networkchange. Networkaddresschanged + =NewNetworkaddresschangedeventhandler(Networkchange_networkaddresschanged );

......

 Void Networkchange_networkaddresschanged ( Object Sender, Eventargs E ){ String Content = String . Format (@ "Cellularmobileoperator {0} iscellulardataenabled {1} iscellulardataroamingenabled {2} isnetworkavailable {3} iswifienabled {4 }" , Devicenetworkinformation . Cellularmobileoperator, // Carrier name, such as "China Mobile"  Devicenetworkinformation . Iscellulardataenabled, Devicenetworkinformation . Iscellulardataroamingenabled, Devicenetworkinformation . Isnetworkavailable, Devicenetworkinformation . Iswifienabled );Networkinterfacelist List = New  Networkinterfacelist (); // Obtain all networkinterfaces  Foreach ( VaR Item In List ){ String TEXT = String . Format ( @ "Bandwidth {0} interfacename {1} interfacestate {2} interfacetype {3 }" , Item. bandwidth,// Unit: kbps (kilobytes per second) Item. interfacename, // Name of the network connection Item. interfacestate, // Enumerate connected/disconnected Item. interfacetype ); // Network type Enumeration Content + = text;} textblock_networkinfo.text = content ;}

After my tests, the network environment changes when the application is switched to the background. After switching back to the foreground, the event can be detected immediately.

 

The networkinterfacetype enumeration in the Microsoft. Phone. net. networkinformation. networkinterface namespace has the following typical values:

    • Wireless80211 -- WiFi
    • Ethernet-USB
    • Mobilebroadbandgsm
    • Mobilebroadbandcdma
    • None
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.