The Battery class is available at Xamarin.Essentials API. It is used to check the device's battery information and monitor it for changes. Android, iOS, and UWP offer unique operating systems and platform APIs that developers have access to all in C# languages, leveraging Xamarin. Xamarin.Essentials provide a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.
In this article, you will learn how to check the battery Information like Battery level, status, and power source in Xamarin Forms applications using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.
The following important tools are required for developing Xamarin Forms App,
- Windows 10 (Recommended)
- Visual Studio 2017
- Android API 19 or higher and UWP 10.0.16299.0 or higher.
Now, we can discuss step by step App development.
Step 1
Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormBatteryState) ->OK.
Step 2
Select the cross-platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, Afterwards, Visual Studio creates 3 projects (Portable, Droid, UWP)
Step 3
For adding a reference, right-click your solution (XamFormBatteryState) and select Manage NuGet Packages.
For adding Xamarin.Essentials Reference, choose to browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.
Step 4
Add the Label controls in Mainpage.Xaml for displaying title and battery Information like battery level, status and power source.
- <Label FontAttributes="Bold" Text="Check the Battery Status in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label x:Name="lblbatstate" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
- <Label x:Name="lblbatlevel" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
- <Label x:Name="lblbatpowersrc" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
Step 5
Add the following code in theAssemblyInfo.csin XamFormBatteryState.Android project under Properties folder,
[assembly: UsesPermission(Android.Manifest.Permission.BatteryStats)]
Step 6
Add the following code in the MainActivity.cs in XamFormBatteryState.Android project,
In onCreate() method,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
- {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 7
Add the following namespace and code in Mainpage.Xaml.cs
- using Xamarin.Essentials;
- public MainPage() {
- InitializeComponent();
- Battery.BatteryChanged += Battery_BatteryChanged;
- }
- void Battery_BatteryChanged(object sender, BatteryChangedEventArgs e) {
- var level = Battery.ChargeLevel;
- var state = Battery.State;
- var source = Battery.PowerSource;
- lblbatlevel.Text = "Battery Level : " + level.ToString();
- lblbatstate.Text = "Battery Status : " + state.ToString();
- lblbatpowersrc.Text = "Battery Powersource : " + source.ToString();
- }
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormBatteryState" x:Class="XamFormBatteryState.MainPage">
- <StackLayout>
- <Label FontAttributes="Bold" Text="Check the Battery Status in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label x:Name="lblbatstate" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
- <Label x:Name="lblbatlevel" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
- <Label x:Name="lblbatpowersrc" Margin="0,0,0,10" HorizontalTextAlignment="Center"></Label>
- </StackLayout>
- </ContentPage>
Step 8
We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormBatteryState.Droid and XamFormBatteryState.UWP (Universal Windows)
Step 9
Deploy your App in UWP and Android. The output of the XamFormBatteryState App is,
UWP with AC power source,
UWP with Battery power source (Disconnecting the AC cable),
Android Emulator with property changing,
After changing the Battery status and charger,
Summary
Now, you have successfully verified the device battery information in a Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.