Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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.

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.

Is there any way to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 += "
";
cost += "Roaming: " + internetConnectionProfile.GetConnectionCost().Roaming + "
";
cost += "Over Data Limit: " + internetConnectionProfile.GetConnectionCost().OverDataLimit + "
";
cost += "Approaching Data Limit : " + internetConnectionProfile.GetConnectionCost().ApproachingDataLimit + "
";

NetworkStatus.Text = cost;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...