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

查看:14
本文介绍了如何检查 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 中设置的数据限制.当提供者不允许使用数据感知 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 += "
";
cost += "Roaming: " + internetConnectionProfile.GetConnectionCost().Roaming + "
";
cost += "Over Data Limit: " + internetConnectionProfile.GetConnectionCost().OverDataLimit + "
";
cost += "Approaching Data Limit : " + internetConnectionProfile.GetConnectionCost().ApproachingDataLimit + "
";

NetworkStatus.Text = cost;

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

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