I don't mean nothing happens. The dialog pops up:
"Let app always run in background? Allow / Deny
But then if I go into settings, App, Battery Optimizations, my app is still set to "Optimizing Battery use". I can then manually switch it to "Don't Optimize", but according to the docs and all other questions on here, using the ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent, and clicking allow, should change this setting.
I am using the:
Also, this app is for research, and will not go on the store, so I am not worried about Play Store policies.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
I am using the above code. It fires, brings up a dialog to "Let app always run in background?" I press Allow.
My question is what pressing "Allow" actually does, because if you then go into the App settings, and look at Battery Optimization, Settings says the Application is still "Optimizing Battery Usage".
So it would appear the above intent, and pressing Allow, does not actually do anything what so ever.
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.