How do I check whether a WP8 device uses wifi, mobile plan or roaming to load data

后端 未结 3 1554
悲&欢浪女
悲&欢浪女 2020-12-20 07:03

I plan to load only as much data as required in my app. Which means, that when the data is loaded via Wifi, I want to prefetch things. If the data is loaded via mobile plan

相关标签:
3条回答
  • 2020-12-20 07:37

    The phone can use both 3G and WiFi at the same time. The case I've tested - the phone haven't close existing TCP connections opened while on 3G after it found and connected to a WiFi network. If a new TCP connection is opened it'll use WiFi instead.

    There's SocketExtensions.GetCurrentNetworkInterface extension method from Microsoft.Phone.dll that tell you which interface is used for the specified socket.

    0 讨论(0)
  • 2020-12-20 07:44

    Have you tried the System.Net.NetworkInformation namespace on WP8. This package has a static method which returns the network status. You can then switch based on this information

    NetworkInterface.NetworkInterfaceType

    More details can be found here

    0 讨论(0)
  • 2020-12-20 07:47

    Let's solve it by myself:

    There is the Data Sense API, which allows one to not only check whether the device is roaming, but also check whether the app approaches or is over the data limit set in Data Sense. The API also works when the provider doesn't allow to usse the Data Sense UI.

    In particular, this code from the link above solves everything!

    // Get current Internet Connection Profile.
    ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
    
    // Check the connection details.
    if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != IANA_INTERFACE_TYPE_WIFI)
    {
        // Connection is not a Wi-Fi connection. 
        if (internetConnectionProfile.GetConnectionCost().Roaming)
        {
            // User is roaming. Don't send data out.
            m_bDoNotSendData = true;
        }
    
        if (internetConnectionProfile.GetConnectionCost().ApproachingDataLimit)
        {
            // User is approaching data limit. Send low-resolution images.
            m_bSendLowResolutionImage = true;
        }
    
        if (internetConnectionProfile.GetConnectionCost().OverDataLimit)
        {
            // User is over data limit. Don't send data out.
            m_bDoNotSendData = true;
        }
    }
    else
    {   
        //Connection is a Wi-Fi connection. Data restrictions are not necessary.                        
        m_bDoNotSendData = false;
        m_bSendLowResolutionImage = false;
    }
    
    // Optionally, report the current values in a TextBox control.
    string cost = string.Empty;
    switch (internetConnectionProfile.GetConnectionCost().NetworkCostType)
    {
        case NetworkCostType.Unrestricted:
            cost += "Cost: Unrestricted";
            break;
        case NetworkCostType.Fixed:
            cost += "Cost: Fixed";
            break;
        case NetworkCostType.Variable:
            cost += "Cost: Variable";
            break;
        case NetworkCostType.Unknown:
            cost += "Cost: Unknown";
            break;
        default:
            cost += "Cost: Error";
            break;
    }
    cost += "\n";
    cost += "Roaming: " + internetConnectionProfile.GetConnectionCost().Roaming + "\n";
    cost += "Over Data Limit: " + internetConnectionProfile.GetConnectionCost().OverDataLimit + "\n";
    cost += "Approaching Data Limit : " + internetConnectionProfile.GetConnectionCost().ApproachingDataLimit + "\n";
    
    NetworkStatus.Text = cost;
    
    0 讨论(0)
提交回复
热议问题