Apparently your extension uses a Foreground service (for the notification). Right?
Are you absolutely sure that ignoring battery optimization is actually necessary to prevent Doze mode? In my opinion, this should work without it. I did not use it in some of my apps (but only a Foreground service) and they work fine in the background / idle mode.
Doze mode is prevented by the Foreground service and not by ignoring the battery optimization. So what happens if you remove the "ignore the battery optimization" part in your extension (Java file)?
Using the TurnOffDozeMode () function, the user can ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS (// if you want to disable doze mode for this package) or ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS (// if you want to enable doze mode).
If the user installs the Service.aia app and does not click the "TurnOffDozemode" button, the app enters in Dozemode because "IsIgnoringBatteryOptimization" is false.
In Dozemode works s = s + 1 but Web1.Url doesn't work.
And again: I doubt that this is responsible for preventing Doze mode, but only the Foreground service of the notification.
In other words, IGNORE_BATTERY_OPTIMIZATION has no permanent effect on Doze (especially not on certain devices, like Galaxy Note8, S8, ...). I've tested this extensively (using @Taifun's extension).
It works for some time (which cannot be precisely predicted) and then slows down and then stops completely. I had the same behavior with streaming (radio) apps. After a while, ignoring the battery optimization will no longer be effective and the network connection, CPU, timer etc. will be throttled or terminated. I tested this on my Galaxy Note8, S8 (Android 9).
This problem does not appear on some other test devices. But Samsung devices are still very common, so I needed a general solution. And I found this (as I said) with the Foreground service.
@Anke
It is not a Foreground service because it does not use startForeground, nor does it use "android.permission.FOREGROUND_SERVICE".
It also needs the help of the MediaPlayer, the old idea, to continue working, this should not be needed by a "normal" Service, so the code indicated on the web of tutoril is more of a service workaround.
Here's an example from Foreground, although I'm afraid it doesn't work the way we want it either.
We install the application on a mobile, I'll call it BLACK DEVICE.
Click on "Start", then on "TurnOffDozemode", then on "Check if is Ignoring Battery Optimization" and on "Minimize this Screen".
We will observe that approximately every second counts one unit.
Now we close the screen, that is, we press the button of the mobile to put it with a black screen, in the sleeping state. So we leave it for 5 ... 10 ... 20 minutes.
In another mobile, I will call it WHITE DEVICE, we load the same application, in this we can do it with the MIT COMPANION or installed.
In this mobile we only press the last button "Send random number", every so often we press this button.
By pressing this button a random number will be sent to CloudDB, this same mobile will receive it and also the BLACK DEVICE, this will speak the number.
If the system works, we can leave the BLACK DEVICE mobile for a long time with a black screen and you will always receive the number sent by the WHITE DEVICE.
I have only tested it on a Xiaomi with Android 9. (Battery consumption with this app: 2.5 % / hour)