zoukankan      html  css  js  c++  java
  • Windows Phone 8 获取与监听网络连接状态(转)

    原文地址:http://www.cnblogs.com/sonic1abc/archive/2013/04/02/2995196.html 

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

    首先在Windows Phone中可以使用 Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType 来获取应用的网络连接状态。

    复制代码
    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; 
    }
    复制代码

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

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

    image

    image

    复制代码
    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); 
    }
    复制代码

    image

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

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

    image

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

    1. 当手机和 Wi-Fi 之间的连接类型改变时。
    2. 当用户进入或离开漫游状态时。
    3. 当 ApproachingDataLimit 或 OverDataLimit 变为 true 时。
    复制代码
    // Code to execute when the application is launching (eg, from Start) 
    // This code will not execute when the application is reactivated 
    private void Application_Launching(object sender, LaunchingEventArgs e) 
    { 
        NetworkInformation.NetworkStatusChanged += (object sener) => 
        { 
            Deployment.Current.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("NetworkStatusChanged"); }); 
        }; 
    }
    复制代码

    image

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

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

    image

    image

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

    image

  • 相关阅读:
    Flash与3D编程探秘(五) 摄像机旋转和移动
    Flash与3D编程探秘(一) Flash与3D空间
    Flash与3D编程探秘(六) 全方位旋转摄像机
    Flash与3D编程探秘(四) 摄像机旋转基础知识
    Flash与3D编程探秘(二) 静态长方体
    奥巴马当选 ALL THINGS ARE POSSIBLE!
    Flash游戏2D反恐精英
    动画编程中关于Time Based和Frame Based运动
    Flash与3D编程探秘(八) 3D物体着色基础知识
    Flash中使用Actionscript画贝塞尔曲线
  • 原文地址:https://www.cnblogs.com/moses/p/3640341.html
Copyright © 2011-2022 走看看