What is the reason the ScheduledExecutorService is not running while the display is off?

Im pretty new to Android and ran into a problem that I would like to understand. The problem is that java.util.concurrent.ScheduledExecutorServiceit doesn't seem to be fulfilling its tasks while the display of my smartphone is turned off. I used java.util.Timer, before which there was no this problem, but switched to ScheduledExecutorServicebecause of the need to wait for the task to complete after stopping Timer. The timer is used Servicein the background.

Note. Using is AlarmManagernot an option because I want to understand the problem with ScheduleExecutorServiceor will use it again Timer(shutdown synchronization).

Here's how the task is planned:

mScheduler = Executors.newSingleThreadScheduledExecutor();
mScheduler.scheduleAtFixedRate(mTask, 1, 1, TimeUnit.SECONDS);

To clarify the purpose: I periodically update the notification, as well as parts of the user interface, this does not need to be done if the device wants to go into sleep mode, but I will need to handle this case. I think that understanding why it ScheduledExecutorServiceworks the way it is compared to Timerwill help me deal with this.

UPDATE:

As recommended by Chris Stratton, I use BroadcastReceiverto receive Intent.ACTION_SCREEN_ONand Intent.ACTION_SCREEN_OFFintentions to pause / resume ScheduledExecutorService, which works like a charm. However, there seems to be another problem with a ScheduledExecutorServicephone call, the execution does not stop, but something seems to be throttled. This only happens if the screen is turned off due to the proximity sensor (in this case, no intentions are accepted) ...

+4
1

, WakeLock, .

PARTIAL WakeLock , - , .

, , ackground.

+1

Source: https://habr.com/ru/post/1527968/


All Articles