- Understanding the Role of XAP Files
- The Windows Phone Capabilities Model
- The Threading Model for XAML-Based Graphics and Animation in Windows Phone
- Understanding the Frame Rate Counter
- The Windows Phone Application Analysis Tool
- Reading Device Information
- Applying the Model-View-ViewModel Pattern to a Windows Phone App
- Property Change Notification
- Using Commands
- Argument Validation
- A Platform-Agnostic Dialog Service
- Consuming Local Web Applications
- Summary
Reading Device Information
The Microsoft.Phone.Info.DeviceStatus class is a static class used to retrieve information about the phone device, such as the device manufacturer, firmware version, and total memory available to your app.
Table 2.3 describes each property of the DeviceStatus class.
TABLE 2.3. DeviceStatus Properties
Name |
Description |
ApplicationCurrentMemoryUsage |
The memory usage of the current application in bytes. |
ApplicationMemoryUsageLimit |
The maximum additional amount of memory, in bytes, that your application process can allocate. |
ApplicationPeakMemoryUsage |
The peak memory usage of the current application in bytes. |
DeviceFirmwareVersion |
The firmware version running on the device. |
DeviceHardwareVersion |
The hardware version running on the device. |
DeviceManufacturer |
The device manufacturer name. |
DeviceName |
The device name. |
DeviceTotalMemory |
The physical RAM size of the device in bytes. |
IsKeyboardDeployed |
If true the user has deployed the physical hardware keyboard of the device. |
IsKeyboardPresent |
If true the device contains a physical hardware keyboard. |
PowerSource |
Indicates if the device is currently running on battery power or is plugged in to an external power supply. |
In the first release of the Windows Phone OS, the DeviceExtendedProperties class was used to retrieve many of the DeviceStatus property values. DeviceExtendedProperties has since been deprecated, and DeviceStatus takes its place for retrieving most device information.
The downloadable sample code contains a DeviceStatusView.xaml page, which displays each of the DeviceStatus properties. The memory related values have been converted from byte values to megabytes to make them more easily comprehendible (see Figure 2.8).
FIGURE 2.8. DeviceStatusView page.
Calculating Available Memory
Windows Phone 8 device manufacturers are obligated to produce phones that have at least 512MB of RAM.
To determine how much memory your app has to work with, use the DeviceStatus.ApplicationMemoryUsageLimit.
For example, if a particular task is estimated at costing an additional 10MB of memory, determining whether the task will exceed the memory usage limit can be calculated as follows:
long
requiredBytesEstimate = 10 * 1048576;/* 1048576 bytes equals 1 megabyte. */
if
(DeviceStatus
.ApplicationMemoryUsageLimit >=DeviceStatus
.ApplicationCurrentMemoryUsage + requiredBytesEstimate) {/* Perform expensive task. */
}
In addition to foreground app memory constraints, background tasks are limited to 6MB of memory. Background tasks and their memory usage requirements are discussed in Chapter 32, “Conducting Background Activities with Scheduled Actions.”
DeviceStatus Events
While DeviceStatus allows you to retrieve device information, it also includes the following two events:
- KeyboardDeployedChanged
- PowerSourceChanged
If the phone device has a hardware keyboard, such as a sliding keyboard, the KeyboardDeployedChanged event allows you to detect when the keyboard is extended.
You can subscribe to the KeyboardDeployedChanged event as shown:
DeviceStatus
.KeyboardDeployedChanged += HandleKeyboardDeployedChanged;
The event handler can be used to determine whether the keyboard is deployed using the DeviceStatus class, as shown:
void
HandleKeyboardDeployedChanged(object
sender,EventArgs
e) {bool
keyboardDeployed =DeviceStatus
.IsKeyboardDeployed; ... }
PowerSourceChanged Event
When the phone device is connected to a user’s computer, it may be a good time to perform some processor-intensive task that could potentially consume a lot of power, which would otherwise flatten the user’s battery. The PowerSourceChangedEvent allows you to detect when the user attaches or detaches an external power supply.
The PowerSourceChanged event can be subscribed to as shown:
DeviceStatus
.PowerSourceChanged += HandlePowerSourceChanged;
The event handler can be used to retrieve the new PowerSource value from the DeviceStatus class, as shown:
void
HandlePowerSourceChanged(object
sender,EventArgs
e) {PowerSource
powerSource =DeviceStatus
.PowerSource; ... }