Checking Battery Status Using Xamarin.Forms
Step 1 :
add the following NuGet Packages to your project.
- Xam.Plugin.Battery
Step 2 :
Use the following Code :
private void btn_Clicked(object sender, EventArgs e)
{
if (CrossBattery.IsSupported)
{
var falta = CrossBattery.Current.RemainingChargePercent; //Current battery level 0 - 100
Lb1.Text = $"PERSENTAGE: {falta.ToString()}";
var status = CrossBattery.Current.Status; // Current status of the battery
Lb1.Text += $"STATUS:{status.ToString()}\n";
var source = CrossBattery.Current.PowerSource; // Currently how the battery is being charged.
Lb1.Text += $"MEDIUM: {source.ToString()}";
CrossBattery.Current.BatteryChanged += Current_BatteryChanged;
}
}
private void Current_BatteryChanged(object sender, Plugin.Battery.Abstractions.BatteryChangedEventArgs e)
{
LblStatus.Text = "STATUS NOW:" + e.Status.ToString()";
LblStatus.Text += "BATTERY:" + e.IsLow.ToString();
}
Comments
Post a Comment