Obtain and listen to network connection status on Windows Phone 8

Source: Internet
Author: User

Currently, mobile phones only rely heavily on the Internet, especially news, Weibo, music, video, VoIP calls, games, and other information applications that are highly factual, however, the information fee in China is still high, and more users are willing to carry out large data volumes of traffic only in WiFi environments, thus saving traffic fees. However, in order to make our applications more considerate and user-friendly, how can we make our applications save money for users? Today, we will introduce how to obtain and listen to the network connection status in Windows phone8.

First, you can use Microsoft. Phone. net. networkinformation. networkinterface. networkinterfacetype in Windows Phone to obtain the network connection status of the application.

 Public   Static   String  Getnetstates (){  VaR Info = Microsoft. Phone. net. networkinformation. networkinterface. networkinterfacetype;  Switch  (Info ){  Case Networkinterfacetype. mobilebroadbandcdma:  Return   "  CDMA  "  ;  Case  Networkinterfacetype. mobilebroadbandgsm:  Return   "  CSM  "  ;  Case  Networkinterfacetype. wireless80211: Return   "  WiFi  "  ;  Case  Networkinterfacetype. Ethernet:  Return   "  Ethernet  "  ;  Case  Networkinterfacetype. None:  Return   " None  "  ;  Default  :  Return   "  Other  "  ;}  //  Return NULL; }

 

Pass the aboveCodeSome people may think it is not enough, because they cannot see whether the current connection is a 2G 3G or even 4G connection, and what is the status of WiFi connection.

At this time, we can access a connection through devicenetworkinformation. resolvehostnameasync to obtain more information.

 

 Public   Void  Getnetname () {devicenetworkinformation. resolvehostnameasync (  New Dnsendpoint ( "  Www.microsoft.com  " , 80  ),  New Nameresolutioncallback (handle => {Networkinterfaceinfo info = Handle. networkinterface; If (Info! = Null  ) {Name = Info. interfacename + "   " + Info. Description + "   "  ;  Switch  (Info. interfacetype ){  Case  Networkinterfacetype. Ethernet: netname = " Ethernet  "  ;  Break  ;  Case  Networkinterfacetype. mobilebroadbandcdma:  Case  Networkinterfacetype. mobilebroadbandgsm:  Switch  (Info. interfacesubtype ){  Case Networkinterfacesubtype. cellular_3g: netname = "  Cellular_3g + 3G  "  ;  Break  ;  Case  Networkinterfacesubtype. cellular_evdo: netname = "  Cellular_evdo + 3G  "  ; Break  ;  Case  Networkinterfacesubtype. cellular_evdv: netname = "  Cellular_evdv + 3G  "  ;  Break  ;  Case  Networkinterfacesubtype. cellular_hspa: netname = " Cellular_hspa + 3G  "  ;  Break  ;  Case  Networkinterfacesubtype. cellular_gprs: netname = "  Cellular_gprs + 2G  "  ;  Break  ;  Case Networkinterfacesubtype. cellular_edge: netname = "  Cellular_edge + 2G  "  ;  Break  ;  Case  Networkinterfacesubtype. cellular_1xrtt: netname = "  Cellular_1xrtt + 2G  " ;  Break  ;  Default  : Netname = "  None  "  ;  Break  ;}  Break  ;  Case Networkinterfacetype. wireless80211: netname = "  WiFi  "  ;  Break  ;  Default  : Netname = "  None  "  ;  Break ;}}  Else  Netname = "  None  "  ; Deployment. Current. Dispatcher. begininvoke (  Delegate () {MessageBox. Show (name + Netname );});  //  MessageBox. Show (netname );  }),  Null  );} 

 

I believe that many of my friends have used the above information again. I will summarize it here. Another scenario is that when the network conditions change, compared to automatically switching from the indoor environment to the outdoor environment, the network is automatically switched to the cellular network connection under WiFi conditions.

In our simulator, we can also simulate this scenario vs-tools-simulation dashboard.

Secondly, we need to register the networkinformation. networkstatuschanged event in the launching event of the app to trigger this event in the following conditions:

    1. When the connection type between the mobile phone and Wi-Fi changes.
    2. When the user enters or leaves the roaming status.
    3. When approachingdatalimit or overdatalimit is set to true.
//Code to execute when the application is launching (eg, from start)//This code will not execute when the application is reactivatedPrivate VoidApplication_launching (ObjectSender, launchingeventargs e) {networkinformation. networkstatuschanged+ = (ObjectSENER) =>{Deployment. Current. Dispatcher. begininvoke (Delegate() {MessageBox. Show ("Networkstatuschanged");});};}

 

In addition, we can obtain the static getinternetconnectionprofile method of the networkinformation class after networkstatuschanged.

 
Networkcosttype costtype = networkinformation. getinternetconnectionprofile (). getconnectioncost (). networkcosttype;

 

 

The application can be optimized based on the data connection status in the following table.

 

You are welcome to communicate with me here or on Sina Weibo.@ Wang Bo _ Nick

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.