WP8 Development Series 2: How to obtain device power information? Power Supply

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.