Introduction:
In the Windows Phone 7.5 era, it is impossible for developers to obtain device power information due to API limitations. Fortunately, WP8 SDK has added support for this, in this way, we can get the percentage of remaining power in the application, and the remaining time.
1. What namespaces are required? What events are handled? Which attribute values are obtained?
Two attributes are used:
Battery. RemainingChargePercent: gets the percentage of remaining power from the phone.
Battery. RemainingDischargeTime: Get the remaining display time of the phone power supply.
Battery. RemainingChargePercentChanged: event processing when the remaining power changes.
2. design logic:
When starting an application, obtain the power object of the current device in the MainPage constructor, and declare the delegate processing when the power usage changes.
Defines a method for updating the user interface. When any power changes, it interacts with the UI in a timely manner.
3. The implementation code is as follows:
using Windows.Phone.Devices.Power;
public partial class SystemPage : PhoneApplicationPage
{
readonly Battery _battery;
public SystemPage()
{
InitializeComponent();
// Obtain the power object of the current device. Note: you do not need to create an object entity.
_battery = Battery.GetDefault();
_battery.RemainingChargePercentChanged += _battery_RemainingChargePercentChanged;
// Update the user interface
UpdateUI();
}
// Update the UI in time when the power percentage changes
void _battery_RemainingChargePercentChanged(object sender, object e)
{
UpdateUI();
}
void UpdateUI()
{
this.tblBatteryChargePercent.Text = string.Format("{0} %", _battery.RemainingChargePercent);
// Display the remaining battery usage time. Note: RenainingDischargeTime can be displayed in multiple formats.
This. tblBatteryDisplayTime. Text = string. Format ("{0} minutes", _ battery. RemainingDischargeTime. TotalMinutes );
}
}
4. the final implementation of the above Code is as follows:
Original article: http://jasonwei.com/archives/457