现在的只能手机对网络的依赖程度都很高,尤其是新闻、微博、音乐、视频、VOIP通话、游戏等 事实性高的信息类应用,但是目前国内的信息费仍然高居不下,更多的用户只有在 WIFI 的环境下才愿意进行大数据量的流量从而节约流量费用。然而为了使我们的应用更加贴心、更加人性化,如何让我们的应用为用户省钱呢?今天为大家介绍下如何在 Windows Phone8 中获取和监听网络连接状态。

首先在Windows Phone中可以使用


  
  1. Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType 

 来获取应用的网络连接状态。


  
  1. public static string GetNetStates()  
  2. {  
  3.     var info = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType; 
  4.  
  5.     switch (info)  
  6.     {  
  7.         case NetworkInterfaceType.MobileBroadbandCdma:  
  8.             return "CDMA";  
  9.         case NetworkInterfaceType.MobileBroadbandGsm:  
  10.             return "CSM";  
  11.         case NetworkInterfaceType.Wireless80211:  
  12.             return "WiFi";  
  13.         case NetworkInterfaceType.Ethernet:  
  14.             return "Ethernet";  
  15.         case NetworkInterfaceType.None:  
  16.             return "None";  
  17.         default:  
  18.             return "Other";  
  19.     }  
  20.     //return null;  

通过以上代码肯定有同学觉得不够,因为不能看到当前的连接究竟是一个 2G 3G 甚至是4G的连接,WIFI 连接的是什么状态。

这时候我们可以通过 DeviceNetworkInformation.ResolveHostNameAsync 来访问一个连接获取更多信息。

p_w_picpath

p_w_picpath


  
  1. public void GetNetName()  
  2. {  
  3.     DeviceNetworkInformation.ResolveHostNameAsync(  
  4.         new DnsEndPoint("www.microsoft.com", 80),  
  5.         new NameResolutionCallback(handle =>  
  6.         {  
  7.             NetworkInterfaceInfo info = handle.NetworkInterface;  
  8.             if (info != null)  
  9.             {  
  10.                 Name = info.InterfaceName + " " + info.Description + " "; 
  11.  
  12.                 switch (info.InterfaceType)  
  13.                 {  
  14.                     case NetworkInterfaceType.Ethernet:  
  15.                         NetName = "Ethernet";  
  16.                         break;  
  17.                     case NetworkInterfaceType.MobileBroadbandCdma:  
  18.                     case NetworkInterfaceType.MobileBroadbandGsm:  
  19.                         switch (info.InterfaceSubtype)  
  20.                         {  
  21.                             case NetworkInterfaceSubType.Cellular_3G:  
  22.                                 NetName = "Cellular_3G + 3G";  
  23.                                 break;  
  24.                             case NetworkInterfaceSubType.Cellular_EVDO:  
  25.                                 NetName = "Cellular_EVDO + 3G";  
  26.                                 break;  
  27.                             case NetworkInterfaceSubType.Cellular_EVDV:  
  28.                                 NetName = "Cellular_EVDV + 3G";  
  29.                                 break;  
  30.                             case NetworkInterfaceSubType.Cellular_HSPA:  
  31.                                 NetName = "Cellular_HSPA + 3G";  
  32.                                 break;  
  33.                             case NetworkInterfaceSubType.Cellular_GPRS:  
  34.                                 NetName = "Cellular_GPRS + 2G";  
  35.                                 break;  
  36.                             case NetworkInterfaceSubType.Cellular_EDGE:  
  37.                                 NetName = "Cellular_EDGE + 2G";  
  38.                                 break;  
  39.                             case NetworkInterfaceSubType.Cellular_1XRTT:  
  40.                                 NetName = "Cellular_1XRTT + 2G";  
  41.                                 break;  
  42.                             default:  
  43.                                 NetName = "None";  
  44.                                 break;  
  45.                         }  
  46.                         break;  
  47.                     case NetworkInterfaceType.Wireless80211:  
  48.                         NetName = "WiFi";  
  49.                         break;  
  50.                     default:  
  51.                         NetName = "None";  
  52.                         break;  
  53.                 }  
  54.             }  
  55.             else  
  56.                 NetName = "None"
  57.  
  58.             Deployment.Current.Dispatcher.BeginInvoke(delegate() { MessageBox.Show(Name + NetName); });  
  59.             //MessageBox.Show(NetName); 
  60.  
  61.         }), null);  

p_w_picpath

相信以上信息 相信已经有很多朋友再应用中已经使用了 我在这里给大家总结一下,其次还有一个场景就是在网络条件发生变化的时候,比在从室内走到室外, WIFI 条件下自动切换到蜂窝网连接。

这里在我们的模拟器中也可以模拟这个场景 VS- tools – simulation dashboard.

p_w_picpath

其次我们要在 APP 的 Launching 的事件中注册监听NetworkInformation.NetworkStatusChanged 事件激发这个事件的条件分别是:

  1. 当手机和 Wi-Fi 之间的连接类型改变时。
  2. 当用户进入或离开漫游状态时。
  3. 当 ApproachingDataLimit 或 OverDataLimit 变为 true 时。

  
  1. // Code to execute when the application is launching (eg, from Start)  
  2. // This code will not execute when the application is reactivated  
  3. private void Application_Launching(object sender, LaunchingEventArgs e)  
  4. {  
  5.     NetworkInformation.NetworkStatusChanged += (object sener) =>  
  6.     {  
  7.         Deployment.Current.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("NetworkStatusChanged"); });  
  8.     };  

p_w_picpath

另外我们可以在 NetworkStatusChanged  之后获取 NetworkInformation 类的静态 GetInternetConnectionProfile方法


  
  1. NetworkCostType CostType = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost().NetworkCostType; 


p_w_picpath

 

p_w_picpath

可以依据下表数据连接状态对应用进行优化处理。

p_w_picpath

欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick