您的位置:首页 > 其它

【window phone 7】如何获取用户和手机的信息

2011-09-20 14:46 651 查看
在我们写程序的时候,经常需要知道谁在使用这个程序或者说是程序运行在什么样的手机上。相比之前使用.NET CF来开发Windows Mobile系统上的应用程序来说,Windows Phone 7中提供的API就方便多了。

要获取用户或者Windows Phone 7的信息,首先需要在程序的WMAppManifest.xml中声明如下段落:

Code Snippet

<Capability Name=“ID_CAP_IDENTITY_DEVICE“/>

<Capability Name=“ID_CAP_IDENTITY_USER“/>

然后我们可以通过Microsoft.Phone.Info命名空间下的两个类“UserExtendedProperties”和“DeviceExtendedProperties”来分别获取用户和设备的一些信息。这两个类里面都只有两个静态方法“GetValue”和”TryGetValue”。你只需要传入想要查找的属性值就可以。

目前来说,UserExtendedProperties里面只能查找到”ANID”这个值,它是将当前系统中的用户身份进行了某些运算之后得到的一个32位的字符串。同时,在模拟器上,或者在真实设备上如果没有绑定Windows Live ID的话,也获取不到任何数据。

而对于DeviceExtendedProperties,可以获取的信息就相对多一些,例如设备的厂商,型号等等。而且也包含当前程序占用/峰值内存。

编写Demo代码如下:

string anid = UserExtendedProperties.GetValue(“ANID”) as string;

System.Diagnostics.Debug.WriteLine(“ANID:”+ anid);

string result = “AnonymousUserId: “ + anid.Substring(2, 32) + System.Environment.NewLine;

result = result + “Manufacturer:” + DeviceExtendedProperties.GetValue(“DeviceManufacturer”).ToString() + System.Environment.NewLine;

result = result + “DeviceName:” + DeviceExtendedProperties.GetValue(“DeviceName”).ToString() + System.Environment.NewLine;

result = result + “DeviceUniqueId:” + DeviceExtendedProperties.GetValue(“DeviceUniqueId”) + System.Environment.NewLine;

result = result + “DeviceFirmwareVersion:” + DeviceExtendedProperties.GetValue(“DeviceFirmwareVersion”).ToString() + System.Environment.NewLine;

result = result + “DeviceHardwareVersion:” + DeviceExtendedProperties.GetValue(“DeviceHardwareVersion”).ToString() + System.Environment.NewLine;

result = result + “DeviceTotalMemory:” + DeviceExtendedProperties.GetValue(“DeviceTotalMemory”).ToString() + System.Environment.NewLine;

result = result + “ApplicationCurrentMemoryUsage:” + DeviceExtendedProperties.GetValue(“ApplicationCurrentMemoryUsage”).ToString() + System.Environment.NewLine;

result = result + “ApplicationPeakMemoryUsage:” + DeviceExtendedProperties.GetValue(“ApplicationPeakMemoryUsage”).ToString();

System.Diagnostics.Debug.WriteLine(result);

this.textBlock1.Text = result;

意的是,代码中所获取的DeviceUniqueId它只是一个Byte[],所以这里的ToString()方法只能获取到它的类型名。

最后得到结果如下:

ANID:A=FB338361532E5A2A295F9834FFFFFFFF&E=a63&W=1
AnonymousUserId: FB338361532E5A2A295F9834FFFFFFFF
Manufacturer:LG
DeviceName:GW910
DeviceUniqueId:System.Byte[]
DeviceFirmwareVersion:0.8.2.3
DeviceHardwareVersion:1.1.0.0
DeviceTotalMemory:236335104
ApplicationCurrentMemoryUsage:8744960
ApplicationPeakMemoryUsage:8744960
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: