How to Access Battery Status in Windows Phone 8

Let us see the procedure for creating a sample application that programmatically accesses the battery level in Windows Phone 8. Create a new project with a valid name. Once everything is ready our project looks as in the following screen.

page name

Now add one Button control to the triggers and show the battery level, change the button name to “Battery Level”.

XAML Code

  1. <phone:PhoneApplicationPage  
  2.     x:Class="BatteryLevelDemo.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.    <Grid x:Name="LayoutRoot" Background="Transparent">  
  16.         <Grid.RowDefinitions>  
  17.             <RowDefinition Height="Auto"/>  
  18.             <RowDefinition Height="*"/>  
  19.         </Grid.RowDefinitions>  
  20.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  21.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  22.             <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  23.         </StackPanel>  
  24.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  25.             <Button x:Name="getbatteryLevel" Content="Battery Level" HorizontalAlignment="Left" Margin="168,153,0,0" VerticalAlignment="Top" Click="getbatteryLevel_Click"/>  
  26.         </Grid>  
  27.     </Grid>  
  28. </phone:PhoneApplicationPage> 

Now go to the code behind page. First we need to add the following reference for getting the battery level.

  1. using Windows.Phone.Devices.Power; 

The Windows.Phone.Device.Power.Battery class can be used to retrieve the battery information in Windows Phone 8.

Now write the following code in the button click event.

C# Code

  1. private void getbatteryLevel_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    string bateryLevel = Battery.GetDefault().RemainingChargePercent.ToString();  
  4.    MessageBox.Show(bateryLevel);  

The following is the sample code snippet to retrieve the battery level in Windows Phone 8.

retrieve battery level

Use Battery.GetDefault().RemainingDischargeTime to get the discharge time .

Run the application by pressing the F5 Key and see the output as shown in the following.

Run the application