Tuesday, January 1, 2013

Windows phone working with Device Information


Device detection API’s available in the Windows Phone 7 SDK to detect the Device information, Memory detection, Keyboard installation information etc.


We can retrieve the below given  list of device information.


S No             Information                                         Description

1                   DeviceUniqueId                                  Gets device Unique ID.
2                   DeviceManufacturer                            Gets Device Manufacture details.
3                   ApplicationCurrentMemoryUsage       Gets Application Memory Usage.
4                   ApplicationPeakMemoryUsage           Gets Application Memory Leak Usage.
5                   DeviceName                                       Gets the Device Name.
6                   DeviceFirmwareVersion                       Gets the Firmware Version.
7                   DeviceHardwareVersion                      Gets the Hardware Version.
8                   DeviceTotalMemory                            Gets the physical Memory RAM Usage.
9                   Power Source                                      Indicates if the device works with power/battery.
10                 Iskeyboardpresent                                Indicates if any physical keyboard is installed 
11                 IsKeyboardDeployed                           Indicates if the user deploy the physical keyboard
12                 ApplicationMemoryUsageLimit             Gets the maximum amount of memory.

1) Please make a project with name WindowsPhoneInformation and copy the following layout to your's  MainPage.xaml


<phone:PhoneApplicationPage
    x:Class="WindowsPhoneInformation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Windows Phone Information" Style="{StaticResource
PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Device Info" Margin="9,-7,0,0" Style="{StaticResource
PhoneTextTitle1Style}"/>
        </StackPanel>
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,23,0,0" Name="ttDeviceInfo"
Text="F5debug - Device Information List" VerticalAlignment="Top" Width="438" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,76,0,0" Name="ttdeviceuniqueid"
Text="Device Unique ID:" VerticalAlignment="Top" Width="176" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,76,0,0"
Name="txtDeviceUniqueID" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,112,0,0" Name="textBlock4"
Text="Device Manufacturer:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,112,0,0"
Name="txtDeviceManufacturer" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,148,0,0" Name="textBlock6"
Text="Device Name:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,148,0,0" Name="txtDeviceName"
Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,184,0,0" Name="textBlock8"
Text="Firmware Version:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,184,0,0"
Name="txtFirmwareVersion" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,220,0,0" Name="textBlock10"
Text="Hardware Version:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,220,0,0"
Name="txtHardwareVersion" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,251,0,0" Name="textBlock12"
Text="Total Memory:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,251,0,0" Name="txtTotalMemory"
Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,287,0,0" Name="textBlock14"
Text="Current Memory:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,287,0,0"
Name="txtCurrentMemory" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,323,0,0" Name="textBlock16"
Text="Peak Memory:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,323,0,0" Name="txtPeakMemory"
Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,394,0,0" Name="textBlock18"
Text="Power Source:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,394,0,0" Name="txtPowerSource"
Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,430,0,0" Name="textBlock20"
Text="Keyboard Installed:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,430,0,0"
Name="txtkeyboardinstalled" Text="" VerticalAlignment="Top" Width="219" />
            <Button Content="Get Details" Height="72" HorizontalAlignment="Left" Margin="46,520,0,0"
Name="button1" VerticalAlignment="Top" Width="359" Click="button1_Click" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,467,0,0" Name="textBlock3" Text=
"Keyboard Deployed:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,467,0,0"
Name="txtKeyboardDeployed" Text="" VerticalAlignment="Top" Width="219" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,359,0,0" Name="textBlock1"
Text="Maximum Memory:" VerticalAlignment="Top" Width="200" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="218,359,0,0"
Name="txtMaximuMemory" Text="" VerticalAlignment="Top" Width="219" />
        </Grid>
    </Grid>
 
</phone:PhoneApplicationPage>

2) Now just write a code snippet on yours  MainPage.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 Microsoft.Phone.Info;
using System.Windows.Threading;
namespace WindowsPhoneInformation
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            txtCurrentMemory.Text = DeviceStatus.ApplicationCurrentMemoryUsage.ToString();
            txtMaximuMemory.Text = DeviceStatus.ApplicationMemoryUsageLimit.ToString();
            txtPeakMemory.Text = DeviceStatus.ApplicationPeakMemoryUsage.ToString();
            txtTotalMemory.Text = DeviceStatus.DeviceTotalMemory.ToString();
            txtDeviceManufacturer.Text = DeviceStatus.DeviceManufacturer.ToString();
            txtDeviceName.Text = DeviceStatus.DeviceName.ToString();
            txtFirmwareVersion.Text = DeviceStatus.DeviceFirmwareVersion.ToString();
            txtHardwareVersion.Text = DeviceStatus.DeviceHardwareVersion.ToString();
            txtKeyboardDeployed.Text = DeviceStatus.IsKeyboardDeployed.ToString();
            txtkeyboardinstalled.Text = DeviceStatus.IsKeyboardPresent.ToString();
            txtPowerSource.Text = DeviceStatus.PowerSource.ToString();
            //txtDeviceUniqueID.Text = "";
        }
    }
}

3) on the emulator you can see the output as follows :)































No comments:

Post a Comment