Issue
I am a beginner with the Xamarin platform. I recently started developing some simple application with the sole purpose of learning the platform. I encountered a "problem". My Cross-platform app on the Android platform (I have not tested it on other platforms) consumes an unusual amount of battery.
For example, in about 2 min of use the app consumes 5% of battery where in comparison the display consumes 6%. I found out that by going into the Battery consumption page (The Android Settings). I suspect this is because I am not setting the event handlers to null (i.e. disposing of them, see the code below).
This is my xaml code:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinSample"
x:Class="XamarinSample.MainPage">
<StackLayout>
<Slider VerticalOptions="CenterAndExpand"
ValueChanged="OnSliderValueChanged"/>
<Label x:Name="valueLabel"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
Text="A Simple Label"
Font="Large"/>
<Button HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
Text="Click me!"
Clicked="OnButtonClick"/>
</StackLayout>
</ContentPage>
And this is the code-behind:
namespace XamarinSample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
}
void OnSliderValueChanged(object sender, ValueChangedEventArgs args)
{
valueLabel.Text = args.NewValue.ToString("F3");
}
async void OnButtonClick(object sender, EventArgs args)
{
Button button = (Button)sender;
await DisplayAlert("Clicked!",
"The button labeled '" + button.Text + "' has been clicked","OK");
}
}
}
What could be the reason for this behaviour?
Solution
First do not try to analyze the battery usage of an app while debugging (or one built-in a debug configuration) as the code executed (runtime and Jit'd) will be different...
Second, you should use Google's Battery Historian
for your app's battery analysis. This is a Google provided/supported Android toolset and IMHO well worth the minimal effort to set it up.
Batterystats collects battery data from your device, and Battery Historian converts that data into an HTML visualization that you can view in your Browser. Batterystats is part of the Android framework, and Battery Historian is open-sourced and available on GitHub at https://github.com/google/battery-historian.
Showing you where and how processes are drawing current from the battery. Identifying tasks in your app that could be deferred or even removed to improve battery life.
Answered By - SushiHangover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.