Reputation: 571
Im fairly new to Android and have encountered a problem, which i would like to understand. The problem is that the java.util.concurrent.ScheduledExecutorService
doesn't seem to issue its tasks while the display of my smartphone is off. I was using a java.util.Timer
before which didn't have this issue, but was transitioning to ScheduledExecutorService
because of the need to wait for the end of the task execution after stoping the Timer
. The Timer is used in a Service
running in the background.
Note: Using AlarmManager
isn't an option, because i want to understand the problem with ScheduleExecutorService
or else will use Timer
again (synchronizing the shutdown).
Here is how the task is scheduled:
mScheduler = Executors.newSingleThreadScheduledExecutor();
mScheduler.scheduleAtFixedRate(mTask, 1, 1, TimeUnit.SECONDS);
To furher clarify the purpose: I am trying to update a Notification as well as parts of the User Interface periodically, this doesn't need to be done if the device wants to go into "sleep" mode, but i would need to handle this case. I think understanding why ScheduledExecutorService
is behavoring the way it does compared to Timer
will help me handle it.
UPDATE:
As recommended by Chris Stratton, i'm using a BroadcastReceiver
to receive the Intent.ACTION_SCREEN_ON
and Intent.ACTION_SCREEN_OFF
Intents to pause/resume the ScheduledExecutorService
, which works like a charm. However there seems to be another issue with the ScheduledExecutorService
while having a phone call, the execution doesn't stop but seems to be throttled somehow. This is only the case if the screen is turned off due to the proximity sensor (no intents are received in this case either)...
Upvotes: 4
Views: 1494
Reputation: 17115
When the screen is off, if no other applications are holding WakeLock, the CPU suspends.
You should use a PARTIAL WakeLock whenever you need something done while screen is off.
And make sure you release it as soon as you've done all the ackground job.
Upvotes: 1