Device Status for Windows Phone
March 22, 2012
You can use the DeviceStatus class to determine status information about the device, such as the total memory of the device, the hardware version of the device, and whether a physical keyboard has been deployed. In addition, you can register for several events that notify your application when certain properties have changed.
In Windows Phone OS 7.0, the DeviceExtendedProperties class was used to query device-specific properties. In Windows Phone OS 7.1, most of the properties in DeviceExtendedProperties were deprecated, and the new DeviceStatus class should be used instead. However, where appropriate, you can still use the properties in DeviceExtendedProperties that are not deprecated.
Note: |
|---|
|
Important Note: |
|---|
See the Device Status Sample at Code Samples for Windows Phone, and How to: Use the DeviceStatus Class for Windows Phone for code examples of how to use this class. |
-
General device information
You can query DeviceStatus properties to receive information about the device, such as total memory, hardware version, and device manufacturer name.
-
Applications that run outside of the lock screen
Certain types of applications, such as a simple alarm clock application that travelers use to display the date and time, benefit the user by running outside of the lock screen. In combination with asking the user for permission, you can use the PowerSource API and the PowerSourceChanged event to know when the phone has been plugged in to an external source of power.
-
Keyboard availability
Your application could choose to support a landscape or portrait mode depending on whether a hardware keyboard is available. You can use the IsKeyboardPresent property, the IsKeyboardDeployed property, and the KeyboardDeployedChanged event for these or other applications.
-
Monitoring memory usage
As you develop your application, you can use the ApplicationCurrentMemoryUsage and ApplicationPeakMemoryUsage properties to monitor memory usage, and the DeviceTotalMemory and ApplicationMemoryUsageLimit properties to determine device and application memory limits. It is not necessary to check the memory usage of your application at extremely small intervals. It is sufficient to occasionally check peak memory usage. If you find that the peak memory usage value crosses the allowable threshold, as described in section 5.2 of Technical Certification Requirements, you may choose to monitor memory usage more finely in order to help diagnose the problem.
If your application is consuming a lot of memory, track the instance count for major objects in your application, such as pages and user controls. If there are more non-finalized instances than expected, you should investigate why these instances are not being released.
-
Applications should not present raw device status information to the user, or attempt to dynamically parse or process the device status information on the device. A recommended use for the DeviceStatus API is to send the raw device status information to a web service in order to generate statistics or usage data. The DeviceName property may also be used to identify a device in order to mitigate known bugs that are specific to that device.
The following example shows one way to monitor your application’s memory usage. It sets up a timer to display the application’s memory usage every 10 seconds.
public partial class MainPage : PhoneApplicationPage
{
DispatcherTimer timer;
// Constructor
public MainPage()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,10);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
try
{
// These are TextBlock controls that are created in the page’s XAML file.
MemoryTextBlock.Text = Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage.ToString();
PeakMemoryTextBlock.Text = Microsoft.Phone.Info.DeviceStatus.ApplicationPeakMemoryUsage.ToString();
}
catch (Exception ex)
{
MemoryTextBlock.Text = ex.Message;
}
}
}
Note:
You can use the
Important Note: