Windows Phone 8 獲取與監聽網絡鏈接狀態(轉)

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

 

 

如今的只能手機對網絡的依賴程度都很高,尤爲是新聞、微博、音樂、視頻、VOIP通話、遊戲等 事實性高的信息類應用,可是目前國內的信息費仍然高居不下,更多的用戶只有在 WIFI 的環境下才願意進行大數據量的流量從而節約流量費用。然而爲了使咱們的應用更加貼心、更加人性化,如何讓咱們的應用爲用戶省錢呢?今天爲你們介紹下如何在 Windows Phone8 中獲取和監聽網絡鏈接狀態。react

首先在Windows Phone中能夠使用 Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType 來獲取應用的網絡鏈接狀態。windows

複製代碼
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 來訪問一個鏈接獲取更多信息。app

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 條件下自動切換到蜂窩網鏈接。less

這裏在咱們的模擬器中也能夠模擬這個場景 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方法spa

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

 

image

 

image

能夠依據下表數據鏈接狀態對應用進行優化處理。.net

image

相關文章
相關標籤/搜索