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