Hi,
I am looking for some solution for disabling battery optimization setting for my application.
When user install app first time and Battery optimization is enable, App should prompt custom popup (Popup text based on user language), and after clicking on button, It should navigate to setting page, there user can disable battery optimization.
Same setting app should save, And should not ask again and again.
Do you want to achieved it like following GIF?
If so, you could achieve it by dependence service.
There is code.
First of all, we should create a interface that will be used in android.
IBatteryInfo.cs
public interface IBatteryInfo { void StartSetting(); bool CheckIsEnableBatteryOptimizations(); }
Then we achieved it in android.
[assembly: Dependency(typeof(BatteryImplementation))] namespace OpenBatteryDemo.Droid { public class BatteryImplementation : IBatteryInfo { public bool CheckIsEnableBatteryOptimizations() { PowerManager pm = (PowerManager)Android.App.Application.Context.GetSystemService(Context.PowerService); //enter you package name of your application bool result =pm.IsIgnoringBatteryOptimizations("com.companyname.OpenBatteryDemo"); return result; } public void StartSetting() { Intent intent = new Intent(); intent.SetAction(Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings); Forms.Context.StartActivity(intent); // StartActivity(intent); } }
In the AndroidManifest.xaml
, do not forget to add this permission.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
In the end, we can use it in PCL.
private async void Btn_Clicked(object sender, EventArgs e) { if (DependencyService.Get<IBatteryInfo>().CheckIsEnableBatteryOptimizations()) { } else { var action = await Application.Current.MainPage.DisplayAlert("open battery settings", "please close battery optimization", "yes", "No"); if (action) { DependencyService.Get<IBatteryInfo>().StartSetting(); } else { } } }
Answers
What is your app doing to drain the battery so much that you have the need to do this?
We're doing constant GPS monitoring of the vehicle plus screen at 80% and don't need to. Makes me wonder.
We are using third party lib "Glympse" for agent live location tracking, When user launch Agent location tracking from our app and stay long with tracking window, Operational system automatically remove app from background.
Do you want to achieved it like following GIF?

If so, you could achieve it by dependence service.
There is code.
First of all, we should create a interface that will be used in android.
IBatteryInfo.cs
Then we achieved it in android.
In the
AndroidManifest.xaml
, do not forget to add this permission.In the end, we can use it in PCL.
Hi LeonLu,
Thanks Man, Your solution has resolved my problem
Great example, something to bear in mind is you need to check your app is on the acceptable use-cases list, otherwise Google Play won't approve you.
Basically unless you're doing something with peripherals, e.g. iBeacons - forget it.
Hi, is there any way that it will go directly to the apps? Thank you
One of my apps just got removed from the Play Store because it declared the "android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" permission in its manifest.
Hi there,
Are you using foreground service for this? I have a requirement for the same but I also need to know and send response to the backend server if the OS kills the process.
Have you any suggestions?
Thanks in advance