與眾不同 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 == 40) result = result.Insert(8, "-").Insert(17, "-").Insert(26, "-").Insert(35, "-"); } return result; } /// <summary> /// 將一個字節數組轉換為十六進制字符串 /// </summary> private string ByteToHexStr(byte[] bytes) { string returnStr = ""; if (bytes != null) { for (int i = 0; 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, 1); 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 >= 34) { result = new Guid(anid.ToString().Substring(2, 32)).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", 80), 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
[源碼下載]