Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

How many times we are in such a condition when our battery is going to die and we have to do some important calls or something else? If we poll for it, the count will be high. If you are a developer and building apps targeting Universal Windows Platform (aka. UWP), you might want to integrate the battery APIs already exposed.

 

This post will cover about the API today and we will learn how more about the API with proper demonstration. Continue to read more on this topic and don’t forget to check other posts on the same topic.

 

How to get the Battery Information in #UWP apps? (www.kunal-chowdhury.com)

 

Microsoft exposed a static class named “PowerManager” under the namespace “Windows.System.Power”, which provides you all the necessary details about the battery status, remaining charge, discharge time and more. Also it exposes few events, which allows you to get the notifications on change of battery status, energy status, power supply status etc. Find below the API details for your reference:

 

namespace Windows.System.Power
{
    public static class PowerManager
    {
        public static BatteryStatus BatteryStatus { get; }
        public static EnergySaverStatus EnergySaverStatus { get; }
        public static PowerSupplyStatus PowerSupplyStatus { get; }
        public static System.Int32 RemainingChargePercent { get; }
        public static TimeSpan RemainingDischargeTime { get; }
 
        public static event EventHandler<System.Object> BatteryStatusChanged;
        public static event EventHandler<System.Object> EnergySaverStatusChanged;
        public static event EventHandler<System.Object> PowerSupplyStatusChanged;
        public static event EventHandler<System.Object> RemainingChargePercentChanged;
        public static event EventHandler<System.Object> RemainingDischargeTimeChanged;
    }
}

 

Here you can see a demonstration of the usage. You can also change it as per your business/app requirement. Use the exposed API that is require to build in your app.

 

 

The BatteryStatus property will allow you to retrieve the current status of the battery. When your system is running on battery power, it will show Discharging. When your battery is completely charged and running on full power supply, the status will show as Idle. When it is running in A.C. power supply and battery is charging, it will show it as Charging.

 

Similarly, the RemainingChargePercent and the RemainingDischargeTime properties will provide you details about percent of charge remaining in your battery and the approx. time it will take to completely drain out the battery. Based on these details, you might want to take care of data or some other business details before it reaches the last stage.

 

Here are few screenshots from the demo app that I have prepared, which you can download (source code link is shared at the end of the post):

 

Universal Windows Platform - Battery API - Discharging (www.kunal-chowdhury.com)

 

Universal Windows Platform - Battery API - Idle (www.kunal-chowdhury.com)

 

Universal Windows Platform - Battery API - Charging (www.kunal-chowdhury.com)

 

 

I hope the post and the shared code will be useful to you and you will be better utilize the API in your app. Also, using this API, you can create some stunning applications for better utilize the battery details. Do share us the URL of the app that you built.

 

Having any queries or suggestions? Drop a line below and I will try to revert back to you shortly. Appolozise in advance for any delay. Don’t just stop here. Read my last post to know “How to turn ON/OFF the Frame Rate Counter in #UWP apps?”.

 

Connect with me on Twitter, Facebook and Google+ to get the updates that I share over there. Subscribe to my blog’s RSS feed and Email Newsletter to receive the new post updates directly deliver to your inbox. Happy coding and Have a great day!!!

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.