Xamarin Essentials Battery
Today we're going to learn how to use the Xamarin Essentials Battery Feature
First you have to download the base proyect here
Open the solution and if you go to Manage NuGet Packages for solution you'll see that there's a NuGet with the name Xamarin.Essentials already installed
So our Proyect is ready to implement the Battery Feature, the Battery Info Feature requires in Android the android.permission.BATTERY_STATS, so just take a look at Android Solution -> Right Click -> Properties -> Android Manifest, and you'll see the permission, if is not checked just click to enable that permission.
Let's code !!!
At the portable proyect -> View folder create a new Content Page called BatteryPage and in the xaml file just copy and paste this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="Essentials.Views.BatteryPage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <ContentPage.Content> <ScrollView> <Grid> <Grid.RowDefinitions> <RowDefinition Height="60" /> <RowDefinition Height="5" /> <RowDefinition Height="1" /> <RowDefinition Height="30" /> <RowDefinition Height="1" /> <RowDefinition Height="30" /> <RowDefinition Height="1" /> <RowDefinition Height="30" /> <RowDefinition Height="1" /> <RowDefinition Height="60" /> <RowDefinition Height="5" /> <RowDefinition Height="1" /> <RowDefinition Height="50" /> <RowDefinition Height="1" /> <RowDefinition Height="50" /> <RowDefinition Height="1" /> </Grid.RowDefinitions> <Label Grid.Row="0" FontSize="25" HorizontalOptions="Center" Text="Battery Stats" TextColor="{StaticResource NavigationPrimary}" VerticalOptions="Center" /> <BoxView Grid.Row="2" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label x:Name="chargeLabel" Grid.Row="3" FontSize="15" HorizontalOptions="Center" TextColor="#348ABF" VerticalOptions="Center" /> <BoxView Grid.Row="4" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label x:Name="chargingStateLabel" Grid.Row="5" FontSize="15" HorizontalOptions="Center" TextColor="#348ABF" VerticalOptions="Center" /> <BoxView Grid.Row="6" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label x:Name="powerSourceLabel" Grid.Row="7" FontSize="15" HorizontalOptions="Center" TextColor="#348ABF" VerticalOptions="Center" /> <BoxView Grid.Row="8" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label Grid.Row="9" FontSize="25" HorizontalOptions="Center" Text="Battery Info Changed" TextColor="{StaticResource NavigationPrimary}" VerticalOptions="Center" /> <BoxView Grid.Row="11" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label x:Name="lastBatteryInfoLabel" Grid.Row="12" FontSize="15" HorizontalOptions="Center" TextColor="#87bdde" VerticalOptions="Center" /> <BoxView Grid.Row="11" BackgroundColor="{StaticResource NavigationPrimary}" /> <Label x:Name="actualBatteryInfoLabel" Grid.Row="14" FontSize="15" HorizontalOptions="Center" TextColor="#348ABF" VerticalOptions="Center" /> <BoxView Grid.Row="13" BackgroundColor="{StaticResource NavigationPrimary}" /> </Grid> </ScrollView> </ContentPage.Content> </ContentPage> |
At the BatteryPage.xaml.cs paste this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace Essentials.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class BatteryPage : ContentPage { public static string lastBatteryState; public static string actualBatteryState; public BatteryPage() { InitializeComponent(); GetBatteryStats(); Battery.BatteryInfoChanged += Battery_BatteryInfoChanged; } private void Battery_BatteryInfoChanged(object sender, BatteryInfoChangedEventArgs e) { var level = e.ChargeLevel; var state = e.State; var source = e.PowerSource; lastBatteryState = actualBatteryState; actualBatteryState = $"{DateTime.Now.ToString("G")} - Level: {level * 100}, State: {state}, Source: {source}"; lastBatteryInfoLabel.Text = lastBatteryState; actualBatteryInfoLabel.Text = actualBatteryState; //actualBatteryInfoLabel } void GetBatteryStats() { var level = Battery.ChargeLevel; // returns 0.0 to 1.0 or 1.0 when on AC or no battery. chargeLabel.Text = $"Charge level: {level*100}%"; var state = Battery.State; chargingStateLabel.Text = $"Charging State: {state}"; switch (state) { case BatteryState.Charging: // Currently charging break; case BatteryState.Full: // Battery is full break; case BatteryState.Discharging: case BatteryState.NotCharging: // Currently discharging battery or not being charged break; case BatteryState.NotPresent: // Battery doesn't exist in device (desktop computer) case BatteryState.Unknown: // Unable to detect battery state break; } var source = Battery.PowerSource; powerSourceLabel.Text = $"Power Source: {source}"; actualBatteryState = $"{DateTime.Now.ToString("G")} - Level: {level*100}, State: {state}, Source: {source}"; actualBatteryInfoLabel.Text = actualBatteryState; switch (source) { case BatteryPowerSource.Battery: // Being powered by the battery break; case BatteryPowerSource.AC: // Being powered by A/C unit break; case BatteryPowerSource.Usb: // Being powered by USB cable break; case BatteryPowerSource.Wireless: // Powered via wireless charging break; case BatteryPowerSource.Unknown: // Unable to detect power source break; } } } } |
Almost dome, we just have to declare a new Flyout Item, so at AppShell.xaml inside the portable proyect at line number: 34 just add this three lines of code:
1 2 3 |
<ShellItem Title="Battery" FlyoutIcon="{ext:ImageResource Essentials.Resources.Images.FlyoutIcon.png}"> <ShellContent ContentTemplate="{DataTemplate local:BatteryPage}" /> </ShellItem> |
That was easy, wasn't it?
Now just debug the app and you'll see the Battery Stats, and also when a change is made.
If you have questions just send me a mesage and I will help you My Web Page
Results
Related Links
Xamarin Essentials NuGet Essentials
Xamarin Essentials Documentation Microsoft Xamarin.Essentials Documentation
Xamarin Essentials Battery Documentation Battery Stats
Completed Solution Essentials Repo