How to Retrieve Remaining Battery Status of WP8 Programmatically?
If you are a WP8Dev and looking for a way to get the battery status of WP8 device, this small yet useful tips will help you on this.- Article authored by Kunal Chowdhury on .
If you are a WP8Dev and looking for a way to get the battery status of WP8 device, this small yet useful tips will help you on this.- Article authored by Kunal Chowdhury on .
If you are looking for a way to retrieve the current battery status of Windows Phone device, first let me inform you that the same is not available for WP7 device. But if you are looking for the same to implement in WP8 device, you can do it now.
Windows Phone 8 SDK exposes an API to retrieve the current battery status of the device which includes remaining percent and time. Let’s discuss more about it.
Windows Phone 8 SDK exposes an API to retrieve the current status of it’s battery i.e. the remaining amount (in %), and the remaining time. Also you will be able to track the remaining value changes using an event handler. The class named “Battery” is a sealed class present in the “Windows.Phone.Devices.Power” namespace and implements the “IBattery” interface.
It exposes GetDefault() method which returns the handle of the default battery. The two properties named RemainingChargePercent and RemainingDischargeTime returns you the remaining charge amount and time respectively. The event handler named RemainingChargePercentChanged will help you to get the charge remaining notification continuously.
Here is the meta data of the Battery class for your reference:
public sealed class Battery : IBattery
{
public static Battery GetDefault();
public int RemainingChargePercent { get; }
public TimeSpan RemainingDischargeTime { get; }
public event EventHandler<object> RemainingChargePercentChanged;
}
If you want to get the remaining status of the Windows Phone 8 device battery, use the said property as shown below to access the data accordingly:
var remainingPercent = Battery.GetDefault().RemainingChargePercent;
var remainingTimeSpan = Battery.GetDefault().RemainingDischargeTime;
Note that, the RemainingChargePercent will return you the remaining value in percentage and RemainingDischargeTime will return you the remaining time in TimeSpan format. In case you want to show in time format, you have to build the string from that properly.
In case you want to show the battery status continuously, implement the event handler and use those exposed properties to get the remaining status.
I hope that, this small but useful tip will help you if you are looking out for an way to get the battery status. Subscribe to my blog’s RSS Feed and Email Newsletter to get regular updates on article, tips & tricks. I am available on twitter, facebook and google+, connect with me there for any technical queries or discussions. Happy coding.
Solution Architect & Former Microsoft MVP
Kunal Chowdhury is an enterprise solution architect and former multi-year Microsoft MVP. He is the author of three technical books: Windows Presentation Foundation Development Cookbook, Mastering Visual Studio 2017, and the Mastering Visual Studio 2019.
He publishes technical and non-technical articles on Kunal-Chowdhury.com.