与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态
原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之设备
- 硬件状态
- 系统状态
- 网络状态
示例
1、演示如何获取硬件的相关状态
HardwareStatus.xaml.cs
/*
* 演示如何获取设备的硬件信息
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using System.Windows.Navigation;
using Microsoft.Phone.Info; namespace Demo.Device.Status
{
public partial class HardwareStatus : PhoneApplicationPage
{
public HardwareStatus()
{
InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text = ""; /*
* DeviceStatus - 用于获取相关的设备信息
*/ lblMsg.Text += "设备制造商:" + DeviceStatus.DeviceManufacturer;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "设备名称:" + DeviceStatus.DeviceName;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "物理内存总计:" + DeviceStatus.DeviceTotalMemory; // 单位:字节
lblMsg.Text += Environment.NewLine; lblMsg.Text += "系统分给当前应用程序的最大可用内存:" + DeviceStatus.ApplicationMemoryUsageLimit; // 单位:字节
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前应用程序占用内存的当前值:" + DeviceStatus.ApplicationCurrentMemoryUsage; // 单位:字节
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前应用程序占用内存的高峰值:" + DeviceStatus.ApplicationPeakMemoryUsage; // 单位:字节
lblMsg.Text += Environment.NewLine; lblMsg.Text += "硬件版本:" + DeviceStatus.DeviceHardwareVersion;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "固件版本:" + DeviceStatus.DeviceFirmwareVersion;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "设备是否包含物理键盘:" + DeviceStatus.IsKeyboardPresent;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "物理键盘是否正在使用:" + DeviceStatus.IsKeyboardDeployed;
lblMsg.Text += Environment.NewLine; /*
* Microsoft.Phone.Info.PowerSource 枚举 - 供电方式
* Battery - 电池
* External - 外接电源
*/
lblMsg.Text += "供电方式:" + DeviceStatus.PowerSource;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "是否支持多分辨率编码视频的平滑流式处理:" + MediaCapabilities.IsMultiResolutionVideoSupported;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "设备标识:" + GetDeviceUniqueId(); // 当物理键盘的使用状态(使用或关闭)发生改变时所触发的事件
DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged); // 当设备的供电方式(电池或外接电源)发生改变时所触发的事件
DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged);
} void DeviceStatus_PowerSourceChanged(object sender, EventArgs e)
{
MessageBox.Show("供电方式:" + DeviceStatus.PowerSource);
} void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e)
{
MessageBox.Show("物理键盘是否正在使用:" + DeviceStatus.IsKeyboardDeployed);
} /// <summary>
/// 获取设备的唯一ID
/// </summary>
private string GetDeviceUniqueId()
{
string result = "";
object uniqueId;
/*
* DeviceExtendedProperties.TryGetValue() - 用于获取设备的唯一ID
*/
if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
{
result = ByteToHexStr((byte[])uniqueId); if (result != null && result.Length == )
result = result.Insert(, "-").Insert(, "-").Insert(, "-").Insert(, "-");
} return result;
} /// <summary>
/// 将一个字节数组转换为十六进制字符串
/// </summary>
private string ByteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = ; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
}
}
2、演示如何获取系统的相关状态
SystemStatus.xaml.cs
/*
* 演示如何获取设备的系统信息
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using System.Windows.Navigation;
using System.Globalization;
using Microsoft.Phone.Info; namespace Demo.Device.Status
{
public partial class SystemStatus : PhoneApplicationPage
{
public SystemStatus()
{
InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text = ""; // System.PlatformID 枚举 - 包括 Win32S, Win32Windows, Win32NT, WinCE, Unix, Xbox, NokiaS60
lblMsg.Text += "系统内核:" + Environment.OSVersion.Platform; // System.PlatformID 枚举
lblMsg.Text += Environment.NewLine; lblMsg.Text += "系统版本:" + Environment.OSVersion.Version; // System.Version 类型的对象
lblMsg.Text += Environment.NewLine; lblMsg.Text += "CLR 版本:" + Environment.Version; // System.Version 类型的对象
lblMsg.Text += Environment.NewLine; lblMsg.Text += "系统自上次启动以来所经过的毫秒数:" + Environment.TickCount;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前语言:" + CultureInfo.CurrentCulture.DisplayName;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss") + " 星期" + "日一二三四五六七".Substring((int)DateTime.Now.DayOfWeek, );
lblMsg.Text += Environment.NewLine; lblMsg.Text += "当前时区:" + "UTC" + DateTimeOffset.Now.ToString("%K");
lblMsg.Text += Environment.NewLine; lblMsg.Text += "主题 - 背景色:" + GetThemeBackground();
lblMsg.Text += Environment.NewLine; lblMsg.Text += "主题 - 主题色:" + GetThemeAccent();
lblMsg.Text += Environment.NewLine; lblMsg.Text += "Live ID:" + GetWindowsLiveAnonymousId();
} /// <summary>
/// 获取当前设备主题的背景色
/// </summary>
private string GetThemeBackground()
{
string background = "";
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"]; if (darkBackgroundVisibility == Visibility.Visible)
background = "深";
else
background = "浅"; return background;
} /// <summary>
/// 获取当前设备主题的主题色
/// </summary>
private string GetThemeAccent()
{
string accent = "";
Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"]; switch (currentAccentColorHex.ToString())
{
case "#FF1BA1E2":
accent = "蓝";
break;
case "#FFA05000":
accent = "褐";
break;
case "#FF339933":
accent = "绿";
break;
case "#FFE671B8":
accent = "粉红";
break;
case "#FFA200FF":
accent = "紫";
break;
case "#FFE51400":
accent = "红";
break;
case "#FF00ABA9":
accent = "青";
break;
case "#FF8CBF26": // (sdk 7.0)
case "#FFA2C139": // (sdk 7.1)
accent = "黄绿";
break;
case "#FFFF0097": // (sdk 7.0)
case "#FFD80073": // (sdk 7.1)
accent = "洋红";
break;
case "#FFF09609":
accent = "橙";
break;
case "#FF1080DD":
accent = "诺基亚蓝";
break;
default:
accent = currentAccentColorHex.ToString();
break;
} return accent;
} /// <summary>
/// 当绑定了 Live 账号后,此方法可以获取到用户的唯一 ID(匿名标识)
/// </summary>
private string GetWindowsLiveAnonymousId()
{
string result = string.Empty;
object anid; if (UserExtendedProperties.TryGetValue("ANID", out anid))
{
if (anid != null && anid.ToString().Length >= )
{
result = new Guid(anid.ToString().Substring(, )).ToString();
}
} return result;
}
}
}
3、演示如何获取网络的相关状态
NetworkStatus.xaml.cs
/*
* 演示如何获取设备的网络信息
*/ using System;
using System.Linq;
using System.Net;
using Microsoft.Phone.Controls; using System.Windows.Navigation;
using Microsoft.Phone.Net.NetworkInformation;
using System.Threading;
using System.Windows; namespace Demo.Device.Status
{
public partial class NetworkStatus : PhoneApplicationPage
{
public NetworkStatus()
{
InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text = "异步获取网络信息,可能较慢。。。";
lblMsg.Text += Environment.NewLine;
lblMsg.Text += Environment.NewLine; ThreadPool.QueueUserWorkItem(DeviceNetworkInformationDemo);
ThreadPool.QueueUserWorkItem(NetworkInterfaceInfoDemo);
ThreadPool.QueueUserWorkItem(ResolveHostNameAsyncDemo);
} private void DeviceNetworkInformationDemo(object state)
{
this.Dispatcher.BeginInvoke(delegate()
{
/*
* DeviceNetworkInformation - 用于获取相关的网络信息
*/ lblMsg.Text += "运营商名称:" + DeviceNetworkInformation.CellularMobileOperator;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "是否启用了手机网络数据连接:" + DeviceNetworkInformation.IsCellularDataEnabled;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "是否启用了手机网络数据漫游:" + DeviceNetworkInformation.IsCellularDataRoamingEnabled;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "是否启用了 WiFi 网络:" + DeviceNetworkInformation.IsWiFiEnabled;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "网络是否可用:" + DeviceNetworkInformation.IsNetworkAvailable;
lblMsg.Text += Environment.NewLine; /*
* NetworkInterfaceType - 网络接口的类型(Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType 枚举)
*/
lblMsg.Text += "数据网络接入类型:" + NetworkInterface.NetworkInterfaceType;
lblMsg.Text += Environment.NewLine;
}); /*
* NetworkAvailabilityChanged - 连接网络、断开网络或更改漫游状态时触发的事件
*/
DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(DeviceNetworkInformation_NetworkAvailabilityChanged);
} void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
{
/*
* NetworkNotificationEventArgs.NetworkInterface - 返回当前的 NetworkInterfaceInfo 对象
*
* NetworkNotificationEventArgs.NotificationType - 返回 Microsoft.Phone.Net.NetworkInformation.NetworkNotificationType 枚举类型的数据
* InterfaceConnected - 已连接
* InterfaceDisconnected - 连接已断开
* CharacteristicUpdate - 连接更新(如打开或关闭漫游)
*/ this.Dispatcher.BeginInvoke(delegate()
{
switch (e.NotificationType)
{
case NetworkNotificationType.InterfaceConnected:
lblMsg.Text += "已连接到:" + e.NetworkInterface.InterfaceName + "(" + e.NetworkInterface.InterfaceType + ")";
break;
case NetworkNotificationType.InterfaceDisconnected:
lblMsg.Text += "已从此断开:" + e.NetworkInterface.InterfaceName + "(" + e.NetworkInterface.InterfaceType + ")";
break;
case NetworkNotificationType.CharacteristicUpdate:
lblMsg.Text += "连接更新(如打开或关闭漫游):" + e.NetworkInterface.InterfaceName + "(" + e.NetworkInterface.InterfaceType + ")";
break;
default:
lblMsg.Text += "未知变化,当前连接为:" + e.NetworkInterface.InterfaceName + "(" + e.NetworkInterface.InterfaceType + ")";
break;
} lblMsg.Text += Environment.NewLine;
});
} private void NetworkInterfaceInfoDemo(object state)
{
/*
* NetworkInterfaceList - NetworkInterfaceInfo 的集合
*
* NetworkInterfaceInfo - 单个网络接口的相关信息
*/ NetworkInterfaceList interfaceList = new NetworkInterfaceList();
NetworkInterfaceInfo interfaceInfo = interfaceList.First(); this.Dispatcher.BeginInvoke(delegate()
{
lblMsg.Text += "网络接口的名称:" + interfaceInfo.InterfaceName;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "网络接口的类型:" + interfaceInfo.InterfaceType; // Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType 枚举
lblMsg.Text += Environment.NewLine; lblMsg.Text += "网络接口的类型的其他信息:" + interfaceInfo.InterfaceSubtype; // Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceSubType 枚举
lblMsg.Text += Environment.NewLine; /*
* InterfaceState - Microsoft.Phone.Net.NetworkInformation.ConnectState 枚举
* Disconnected, Connected
*/
lblMsg.Text += "网络接口的状态:" + interfaceInfo.InterfaceState;
lblMsg.Text += Environment.NewLine; lblMsg.Text += "网络接口的速度:" + interfaceInfo.Bandwidth; // 单位:Kb/s
lblMsg.Text += Environment.NewLine; lblMsg.Text += "网络接口的说明:" + interfaceInfo.Description;
lblMsg.Text += Environment.NewLine; /*
* Characteristics - Microsoft.Phone.Net.NetworkInformation.NetworkCharacteristics 枚举
* None - 没有什么特殊属性
* Roaming - 漫游
*/
lblMsg.Text += "网络接口的属性:" + interfaceInfo.Characteristics;
lblMsg.Text += Environment.NewLine;
});
} private void ResolveHostNameAsyncDemo(object state)
{
/*
* DeviceNetworkInformation.ResolveHostNameAsync(DnsEndPoint endPoint, NetworkInterfaceInfo networkInterface, NameResolutionCallback callback, Object context) - 异步解析指定的主机名(ping)
* DnsEndPoint endPoint - 主机名
* NetworkInterfaceInfo networkInterface - 解析主机名所使用的网络接口(可以不指定)
* NameResolutionCallback callback - 解析主机名完成后的回调,委托的参数为 NameResolutionResult 类型
* Object context - 异步过程中的上下文
*
* NameResolutionResult - 主机名解析完成后的结果
* AsyncState - 上下文对象
* HostName - 提交解析的主机名
* NetworkErrorCode - 结果状态(Microsoft.Phone.Net.NetworkInformation.NetworkError 枚举)
* NetworkInterface - 用于解析主机名的网络接口
* IPEndPoints - 解析结果,获取到指定主机名的 IP
* 注:如果被解析的主机名是个 cname 的话,那么这里会获得两个 IP,第一个ip是cname服务器的,第二个ip是cname所映射到的服务器的
*/ DeviceNetworkInformation.ResolveHostNameAsync
(
new DnsEndPoint("www.baidu.com", ),
new NameResolutionCallback(nrc =>
{
if (nrc.NetworkErrorCode == NetworkError.Success)
{
this.Dispatcher.BeginInvoke(delegate()
{
foreach (IPEndPoint ipEndPoint in nrc.IPEndPoints)
{
lblMsg.Text += "www.baidu.com - ip:" + ipEndPoint.ToString();
lblMsg.Text += Environment.NewLine;
}
});
}
}),
null
);
}
}
}
OK
[源码下载]
与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态的更多相关文章
- 与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频)
原文:与众不同 windows phone (22) - Device(设备)之摄像头(硬件快门, 自动对焦, 实时修改捕获视频) [索引页][源码下载] 与众不同 windows phone (22 ...
- 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器
原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页][源码下载] 与众不同 windows phone (18) - Device ...
- 与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频)
原文:与众不同 windows phone (21) - Device(设备)之摄像头(拍摄照片, 录制视频) [索引页][源码下载] 与众不同 windows phone (21) - Device ...
- 与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器
原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...
- 与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API
原文:与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API [索引页][源码下载] 与众不同 windows phone (19) - Dev ...
- 与众不同 windows phone (2) - Control(控件)
原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...
- 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏
原文:与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏 [索引页][源码下载] 与众不同 wind ...
- Win10 驱动装不上,提示:Windows 无法验证此设备所需的驱动程序的数字签名。该值受安全引导策略保护,无法进行修改或删除。
Windows 无法验证此设备所需的驱动程序的数字签名.某软件或硬件最近有所更改,可能安装了签名错误或损毁的文件,或者安装的文件可能是来路不明的恶意软件.(代码52) 最近换了新主板,升级了Windo ...
- 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask
[源码下载] 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirec ...
随机推荐
- html+css实现图片的层布局
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android dialog 问题
1.dialog.dismiss和dialog.cancel的区别 Cancel the dialog. This is essentially the same as calling dismiss ...
- Quartz中文文档使用
Quartz中文使用说明文档,内容相当详细,有需要的码友们可以看看!! 好东西要分享!! 下面是文档的内容目录,附上下载的地址:点击打开链接,下载文档 中文版目录总汇及内容提要 第一章. 企业应用中的 ...
- java--函数练习
/* 1,定义一个功能,用于打印矩形 1,定义一个打印九九乘法表功能的函数 */ class FunctionTest { public static void main(String[] args) ...
- android的四种加载模式
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...
- wamp安装后打开默认网页显示dir,图标红点
首先网页显示dir,是因为服务这些没启动,跟图标红点是一个原因,解决了图标红点,就能解决只显示dir的问题. 先测试是不是端口占用问题,如图: 如果是,那就继续往下走. 单击图标左键(记住是左键),然 ...
- linux登录windows服务器
在公司同时也兼顾了王老师会议网站的任务,我喜欢用linux,而会议网站托管在windows系统上,虽然装了双系统,但我还是比较懒,不喜欢经常切换系统.还好,linux可以实现登录windows服务器. ...
- 正确处理Windows电源事件
简介为系统挂起与恢复而进行的应用准备步骤 曾几何时,当您正要通过应用提交或发布一些重要数据时,突然遇到一些急事需要处理,而且会耽误很长时间.当您完成任务回到电脑前时,发现电脑已经自动进入 了挂起状态, ...
- VS2008+Qt 项目目录
1.项目开发环境:VS2008,QT4.7 2.项目的目录: 1)PETCT是解决方案名字 2)Bin目录存放所有动态链接库和执行档,包括自己的产出和第三方库,区分Release和Debug两个版本. ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...