Sunday, June 12, 2011

Mango : Using DeviceStatus in Windows Phone 7.1

First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I raised there and I will soon write more about it in future.Today I am going to share information on DeviceStatus API and how it helps to give more information about Windows Phone Device.

Information :

DeviceStatus is a static class and you can find the same in Microsoft.Phone.Info Namespace. DeviceStatus exposes bunch of static properties and two events of keyboard and powersource respectively. Properties are like below :

  • ApplicationCurrentMemoryUsage
  • ApplicationPeakMemoryUsage
  • DeviceFirmwareVersion
  • DeviceHardwareVersion
  • DeviceManufacturer
  • DeviceName
  • DeviceTotalMemory
  • PowerSource

and two more boolean properties with events I mentioned above.

XAML :

DeviceStatusDesign

I have taken TextBlocks to display information as shown above which I am assigning from Code Behind,So XAML will be like this :

<!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <TextBlock FontWeight="Bold" x:Name="txtApplicationCurrentMemoryUsage" Height="30" HorizontalAlignment="Left" Margin="0,62,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtApplicationPeakMemoryUsage" Height="30" HorizontalAlignment="Left" Margin="0,118,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtDeviceFirmwareVersion" Height="30" HorizontalAlignment="Left" Margin="0,172,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtDeviceHardwareVersion" Height="30" HorizontalAlignment="Left" Margin="2,227,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtDeviceManufacturer" Height="30" HorizontalAlignment="Left" Margin="0,281,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtDeviceName" Height="30" HorizontalAlignment="Left" Margin="6,331,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtDeviceTotalMemory" Height="30" HorizontalAlignment="Left" Margin="6,383,0,0" VerticalAlignment="Top" Width="450" />

            <TextBlock FontWeight="Bold" x:Name="txtPowerSource" Height="30" HorizontalAlignment="Left" Margin="6,435,0,0" VerticalAlignment="Top" Width="450" />

        </Grid>

C# Code :

General Declaration :

DispatcherTimer dptm = new DispatcherTimer();

Constructor :

// Constructor
public MainPage()
{
    InitializeComponent();
    dptm.Interval = new TimeSpan(0, 0, 5);
    dptm.Tick += new EventHandler(dptm_Tick);
    dptm.Start();
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

I have added DispatcherTimer just to get real time memory status and nothing else.

void dptm_Tick(object sender, EventArgs e)
{
     txtApplicationCurrentMemoryUsage.Text = "Application Current Memory Usage : " + Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage.ToString();
     txtApplicationPeakMemoryUsage.Text = "Application Peak Memory Usage : " + Microsoft.Phone.Info.DeviceStatus.ApplicationPeakMemoryUsage.ToString();
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
     WP7DeviceStatus();
}

public void WP7DeviceStatus()
{
     txtDeviceFirmwareVersion.Text = "Firmware Version : " + Microsoft.Phone.Info.DeviceStatus.DeviceFirmwareVersion.ToString();
     txtDeviceHardwareVersion.Text = "Hardware Version : " + Microsoft.Phone.Info.DeviceStatus.DeviceHardwareVersion.ToString();
     txtDeviceManufacturer.Text = "Manufacturer : " + Microsoft.Phone.Info.DeviceStatus.DeviceManufacturer.ToString();
     txtDeviceName.Text = "Device Name : " + Microsoft.Phone.Info.DeviceStatus.DeviceName.ToString();
     txtDeviceTotalMemory.Text = "Total Memory : " + Microsoft.Phone.Info.DeviceStatus.DeviceTotalMemory.ToString();
     txtPowerSource.Text = "Power Source : " + Microsoft.Phone.Info.DeviceStatus.PowerSource.ToString(); 
}

finally this will look like this on Device, As it is running on Emulator hence most of the information will be change after it goes on actual device.

Output :

DeviceStatus

Remember that its always good to notify user that your application is recording this information if at all your application is storing this information somewhere for some kind of analysis is getting perform on the same or using this data for application. Soon I will be posting full end to end application there I will be showing the exact use of this DeviceStatus.However I hope you will explore it and start using it in your Windows Phone application.As I shown above,it is very simple to implement. I am soon posting some advance tutorials on Mango in coming days. I don’t want to push to much which will create confusion for you, So I am posting basic articles to start with and then we will slowly go ahead at different higher levels.

So try out this API and I will be back soon with some more interesting demos on Mango, There is lot to show and talk on Mango and I am at background exploring lot many things,so keep visiting here as I will be posting some good stuff on Mango in coming days.

Vikram.

No comments: