如何检查WP8设备是否使用wifi,移动计划或漫游来加载数据 [英] How do I check whether a WP8 device uses wifi, mobile plan or roaming to load data

查看:85
本文介绍了如何检查WP8设备是否使用wifi,移动计划或漫游来加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 or even roaming, I would like to ask the user.

但是,我只找到了Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation,它为我提供了有关可用内容而不是实际使用内容的反馈. NetworkInterface.GetInternetInterface()也可以使用,但是没有提供有关是否漫游的详细信息.

However, I only found Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation which gives me feedback on what is available, not what is actually used. NetworkInterface.GetInternetInterface() also works, but does not give me details on whether it's roaming or not.

有什么办法吗?

推荐答案

让我自己解决它:

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

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;

这篇关于如何检查WP8设备是否使用wifi,移动计划或漫游来加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆