如何检查WP8设备是否使用wifi,移动计划或漫游来加载数据

sib*_*bbl 1 c# networking windows-phone windows-phone-8

我计划只加载我的应用程序所需的数据.这意味着,当通过Wifi加载数据时,我想预取东西.如果数据是通过移动计划甚至漫游加载的,我想询问用户.

但是,我只找到了Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation,它向我提供了有关可用内容的反馈,而不是实际使用的内容.NetworkInterface.GetInternetInterface()也可以,但不会给我详细说明它是否正在漫游.

有什么办法吗?

sib*_*bbl 7

让我们自己解决一下:

有一个Data Sense API,它不仅可以检查设备是否在漫游,还可以检查应用程序是否接近或超过Data Sense中设置的数据限制.当提供程序不允许使用Data Sense UI时,API也可以使用.

特别是,上面链接中的代码解决了一切!

// 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;
Run Code Online (Sandbox Code Playgroud)