Retreiving the Remaining time of the Windows Phone 8 device battery programatically using C#

In my previous blog post , i explained How to retreive the Battery Level programatically in Windows Phone 8 ?

In this blog post , i will explain how the Windows Phone developers could retreive the estimated or the remaining time of device battery programatically using C#.

How to retreive the Remaining time of the Windows Phone 8 device battery programatically using C# ?

The Battery class includes the property RemainingDischargeTime which is a read only property that gets the time left for the windows phone device battery to be fully discharged.

Get the access to the Battery instance with the Battery.GetDefault() and then use the property RemainingDischargeTime to retreive the time left.

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;

namespace PhoneApp3
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // SK - To display the Remaining time
            MessageBox.Show(Windows.Phone.Devices.Power.Battery.GetDefault().RemainingDischargeTime.TotalMinutes.ToString());

        }

    }
}

Note that when you use the the RemainingDischargeTime , it will show you a invalid remaining time but should work fine on the windows phone device.

The following two tabs change content below.
Senthil Kumar is a Speaker , Blogger and Software Engineer from Bangalore. He is a Microsoft MVP in Client Development. He primarily works on .NET technologies like C# , ASP.NET MVC , Entity Framework , SQL Server etc. He is a Windows Phone Enthusiast and writes about Windows Phone and Mobile development at MobileOSGeek.com.
Pin It

Leave a Reply