Extension with Rush. Keep the app running. Doze mode. Service. Notification. Source code

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.

In my sample app Servicio.aia, I have a counter s = s + 1 and an internet call Web1.Url: http: //worldtimeapi.org ......

If I don't disable the Doze mode I get

I am talking about the battery optimization.

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).

Not on my test devices (Galaxy ...).

@Anke

Battery optimization is Doze mode.
Battery optimization (Doze mode) - Taming The Droid.

I have only tried this extension installed on Xiaomi Android 9, it would be interesting for other users to try it on other versions of Android (26+)

This extension does not use FOREGROUND_SERVICE

Ok, but why doesn't Taifun's extension work then?

See e.g. here:

@Anke

An app that is whitelisted can use the network (in my extension Web.Url) and hold partial wake locks during Doze (MediaPlayer.setLooping running).

the weird thing is that s = s + 1 doesn't work for you

From this link:

I would like to understand if it is really possible to prevent Doze (completely) without a (Foreground) Service.

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.

I'd really like to understand how it works.

So which service of the following do your extension use?

grafik

@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.

2 Likes

9.- Testing with Bluetooth HC-06.

ServicioBluetooth.aia (106.1 KB)

  • I have only tested it on a Xiaomi with Android 9. It works, that is, it maintains the connection even with the screen turned off.
  • Arduino sends by Bluetooth 1 2 3 4 5 6 7 8...
  • App receives those numbers and shows them on the screen (and in Notification every 10)

bluetooth_Ser

#include <SoftwareSerial.h>
SoftwareSerial BT(2,3);

int k = 0;
String kString = "0";

void setup(){
 BT.begin(9600);
 Serial.begin(9600);
}
 
void loop(){
  k = k + 1;
  Serial.println(k);
  kString = (String) k + '\n';
  BT.write(kString.c_str());
  delay(1000);
}

(TODO, check battery consumption in hours.)

3 Likes

Thanks for the explanation.
I checked whether a streaming app was also running in the background (idle mode). And it works.

I will check later whether the ExoPlayer also works in Kodular with it. If so, it would be great. See here:

The question that remains is whether it will also work without notification?

10.- A mobile sends information to another that is with the black screen.

ServicioCuenta.aia (109.0 KB)

  • 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)

3 Likes

file "servicio.aia" è Free?

File servicio.aia is free.