Google Windows Phone 8 Battery API:Retreiving the Remaining time of device battery c# | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Wednesday 13 August 2014

Windows Phone 8 Battery API:Retreiving the Remaining time of device battery c#

Introduction:

The Battery class is available from "Windows.Phone.Devices.Power" namespace which 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.The Battery API makes a nice addition to the WP8 SDK.I can see in the near future that some applications will display the battery metrics on a Live Tile or in the application that hides the System Tray.
The Battery API is pretty easy to use:
  • Get the Battery instance with Battery.GetDefault().
  • Bind to the event RemainingChargePercentChanged.
The battery properties are:
  • RemainingChargePercent: gets a value that indicates the percentage of the charge remaining on the phone’s battery.
  • RemainingDischargeTime: gets a value that estimates how much time is left until the phone’s battery is fully discharged. 
With the following code:
using Windows.Phone.Devices.Power;

namespace BatteryApp
{
    public partial class MainPage
    {
        private readonly Battery _battery;

        public MainPage()
        {
            InitializeComponent();

            _battery = Battery.GetDefault();

            _battery.RemainingChargePercentChanged += OnRemainingChargePercentChanged;

            UpdateUI();
        }

        private void OnRemainingChargePercentChanged(object sender, object e)
        {
              textBlockRemainingCharge.Text = string.Format("{0} %", _battery.RemainingChargePercent);                           textBlockDischargeTime.Text = string.Format("{0} minutes", _battery.RemainingDischargeTime.TotalMinutes);
        }
        
    }
}
Output:

Note: When running this on the emulator, it will show 100% and an infinite discharge time. The above values are fake.

FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.

Follow me always at  
Have a nice day by  :)

1 comment:

  1. The Battery API makes a nice addition to the WP8 SDK.I can see in the near future that some applications will display the battery metrics on a Live Tile or in the application that hides the System Tray. Radio batteries

    ReplyDelete

Search Engine Submission - AddMe