How do I properly fire ACTION_REQUEST_IGNORE_BATTERY_
OPTIMIZATIONS intent?
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
Kotlin
val intent = Intent()
val pm : PowerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
if (pm.isIgnoringBatteryOptimizations(context.packageName)) {
intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
} else {
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)
See this answer for more information.
ACTION_REQUEST_IGNORE_BATTERY_
OPTIMIZATIONS does nothing
I put your code in a scrap project, added the android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission to the manifest, ran it on a Google Pixel (Android 9.0), and everything worked as expected. No reboot required.
So, either:
Your problem is only on Android 8.1 or older devices and reflects a platform bug that they fixed in 9.0, or
Your problem is tied to certain devices from certain manufacturers
My guess is that it is the latter, but you would need to do more extensive testing to try to determine that.
Android M startActivity battery optimization
To open list of apps for choosing battery optimization you can use this code sample:
private void openPowerSettings(Context context) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
context.startActivity(intent);
}
It doesn't need to have <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
permission, so it should be ok to publish it to Google Play (for details please check this thread and comments to this question).
NOTE
Adding this line
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
will cause crash "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }". User has to find the app in the list, it seems no way to jump directly to our app.
Xamarin Forms - How to check setting of RequestIgnoreBatteryOptimizations permission on Android
From this document, there are two ways to set Battery Optimization
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
intent to take the user directly to the Battery Optimization
, where they can add the app.
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission can trigger a system dialog to let the user add the app to the exemption list directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
Intent to trigger the dialog.
I use PowserManager.isIgnoringBatteryOptimizations to check Battery Optimization.
Firstly, add RequestIgnoreBatteryOptimizations
in AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
Then creating Interface in Shared code.
public interface IBattery
{
void getbattery();
}
Implementing this interface in Android platform.
[assembly: Dependency(typeof(ImplementBattery))]
namespace FormsSample.Droid
{
public class ImplementBattery : IBattery
{
public void getbattery()
{
Intent intent = new Intent();
String packageName = MainActivity.mac.PackageName;
PowerManager pm = (PowerManager)MainActivity.mac.GetSystemService(Context.PowerService);
if (pm.IsIgnoringBatteryOptimizations(packageName))
intent.SetAction(Android.Provider.Settings.ActionIgnoreBatteryOptimizationSettings);
else
{
intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations);
intent.SetData(Android.Net.Uri.Parse("package:" + packageName));
}
MainActivity.mac.StartActivity(intent);
}
}
}
Creating static Mainactivity field in Mainactivity.cs.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static MainActivity mac;
protected override void OnCreate(Bundle savedInstanceState)
{
initFontScale();
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
mac = this;
Now, using DependencyService to fire.
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IBattery>().getbattery();
}
Remove app from whitelist in android marshmallow
I found the solution why I didn't find disable button to remove or optimizate battery. Because such button missed in FlymeCover. The FlymeCover ignore android white-listed and use own optimization.
Related Topics
Rotating Image. Animation List or Animated Rotate? (Android)
Listfragment Does Not Accept My Layout
Phone Number Validation Android
Imageview Adjustviewbounds Not Working
How to Change Android:Windowsoftinputmode Value from Java Class
Handle Screen Orientation Changes When There Are Asynctasks Running
Ionic Cordova App Stopped Compiling After Google's June 17Th Firebase Sdk Update
Android, How Do How to Get a List of All Files in a Folder
Android Studio and Gradle Build Error
Android 4.4.2 - Java.Lang.Runtimeexception: Performing Stop of Activity That Is Not Resumed
Does Android Service Run from a Separated Thread Instead of UI
Android How to Wait Until a Service Is Actually Connected
How to Launch Android Calendar Application Using Intent (Froyo)