Search code examples
windows-phone-8triggersbatterylevelbackground-task

Trigger task when battery is about a specific percentage


For confidential reasons, I cannot explain all the context of my problem. Through a windows phone app, I would like to let users choose a battery status to trigger a specific task. For example, the user choosed that a task have to be triggered when the battery is about 30% of charge. Users can choose plenty of battery status to trigger a task. These choosed percentages will be inserted in local database. Does somebody knows how to do that ?


Solution

  • In Your case you can use RemainingChargePercentChanged Event of Battery class. The Event Occurs when the value of RemainingChargePercent decreases by 1%.

    public MainPage()
    {
        InitializeComponent();
        Battery _battery = Battery.GetDefault();
        _battery.RemainingChargePercentChanged += _battery_RemainingChargePercentChanged;
    }
    
    
    void _battery_RemainingChargePercentChanged(object sender, object e)
    {
        Battery _battery = Battery.GetDefault();
        MessageBox.Show(_battery.RemainingChargePercent.ToString());
    }
    

    You can have more Reference here Battery Class