惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园_首页
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
云风的 BLOG
云风的 BLOG
腾讯CDC
V
Visual Studio Blog
宝玉的分享
宝玉的分享
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
月光博客
月光博客
T
Threatpost
Recent Announcements
Recent Announcements
G
Google Developers Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
V
V2EX
P
Proofpoint News Feed
Y
Y Combinator Blog
The Register - Security
The Register - Security
N
News | PayPal Newsroom
L
Lohrmann on Cybersecurity
H
Hacker News: Front Page
MongoDB | Blog
MongoDB | Blog
量子位
T
Troy Hunt's Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - MainTao

WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置 WebStorm安装Vim以及快捷键设置 scroll clock–jQuery widget implement 基于iScroll 的开发(不断补充) C#操作Win7/Win8的库、收藏夹 P/Invoke Interop 实例 WP7图片从独立存储绑定 编写jQueryUI插件(widget) 调试Javascript Blend制作动画 用OpacityMask快速制作theme friendly UI 用gradient brush和OpacityMask实现fade edge效果 blend 画图 ControlTemplate & DataTemplate PhoneApplicationFrame以及设置Obscured/Unobscured的event handler Windows Phone 7 短信表情 WP7应用程序生命周期,以及各个event handler分别该做什么 第一个WP7程序 开发总结 XAML中的空格、换行、Tab
Windows Phone 7监测网络环境变化
MainTao · 2012-01-03 · via 博客园 - MainTao

有些应用是跟网络环境敏感的,WiFi快且免费,3G走流量,GSM慢,所以我们需要知道如何判断当前的网络类型,以及当网络类型发生改变时如何得到通知。

MSDN相关文章:How To: Detect Network Changes

下面我简单介绍一下。

当网络发生变化时,会引发网络地址发生变化,要监测这一事件,需要用到位于System.Net.NetworkInformation命名空间下的NetworkChange类。

要查看网络连接的类型和状态,用到的类都在Microsoft.Phone.Net.NetworkInformation这个命名空间里。

注意上面两个不同的命名空间,Microsoft.Phone.Net.NetworkInformation是专门用于手机,System.Net.NetworkInformation用于各种场合。由于这两个类中的名字冲突很多,一起用的时候注意区分好。

网络环境变化时得到通知

只需要监听一个事件:

NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

……

void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
    string content = string.Format(
            @"CellularMobileOperator        {0}
            IsCellularDataEnabled           {1}
            IsCellularDataRoamingEnabled    {2}
            IsNetworkAvailable              {3}
            IsWiFiEnabled                   {4}",
            DeviceNetworkInformation.CellularMobileOperator, // 运营商名字,例如“中国移动”
            DeviceNetworkInformation.IsCellularDataEnabled,
            DeviceNetworkInformation.IsCellularDataRoamingEnabled,
            DeviceNetworkInformation.IsNetworkAvailable,
            DeviceNetworkInformation.IsWiFiEnabled);
 
    NetworkInterfaceList list = new NetworkInterfaceList(); // 获得所有NetworkInterfaces

    foreach (var item in list)
    {
        string text = string.Format(
            @"
            Bandwidth       {0}
            InterfaceName   {1}
            InterfaceState  {2}
            InterfaceType   {3}
            ",
            item.Bandwidth, // 单位是Kbps(千比特每秒)
            item.InterfaceName, // 网络连接的名字
            item.InterfaceState, // 枚举 Connected/Disconnected
            item.InterfaceType); // 网络类型的枚举
        
        content += text;
    }

    textBlock_NetworkInfo.Text = content;
}

经过我的测试,应用被切换到后台时网络环境发生了变化,在切换回前台运行后,也一样能立即侦测到这个事件。

在Microsoft.Phone.Net.NetworkInformation.NetworkInterface命名空间下的NetworkInterfaceType枚举,比较典型的值有:

  • Wireless80211  -- WiFi
  • Ethernet   -- USB
  • MobileBroadbandGSM
  • MobileBroadbandCDMA
  • None