0

I have a problem (that's why I'm here :P):

I am running a TimerTask and/or a Handler. They should do something every second, no matter if the screen is on or not (standby). The problem is, after some time (2 to 10 hours) this process becomes weird timed. Sometimes it takes 10 seconds, sometimes 4 hours etc.

Now, I've read that you can use a Partial Wake Lock to solve this issue. Tried it, but it has not solved my issue (Maybe you should know that another library is also using a WakeLock which gets released after some time, but mine never gets released by me).

Maybe you should also know that the task/runnable runs on an asynctask (so on it's own thread). The wakelock is created from outside.


Edit:

Maybe it's good to know that it's a device owner app. Also, I know of the battery drain problem, but I still need it. The app really has to process this every second. I just need a solution for it, any, no matter which.


Edit 2:

Here's my current WakeLock code, which is started when my custom application reaches onCreate. As I said, it is never released.:

    PowerManager mgr = (PowerManager)getApplicationContext().getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"CustomWakeLock");
    wakeLock.acquire();
Antonio Vlasic
  • 337
  • 3
  • 15
  • `Timer` are very expensive in terms of CPU. And why do you have to do task on each second even if device is on standby. However i am quite confused here if you are talking about a background working(Service) or forground(Activity). Provide the full explanation of problem with code. – ADM Jan 17 '18 at 06:59
  • @ADM `Timer` is just a `Thread` that calls `wait()` in its main loop - so it is not that `"very expensive in terms of CPU"` - it does not [busy wait](https://en.wikipedia.org/wiki/Busy_waiting) – pskink Jan 17 '18 at 07:05
  • Thx for clarification @pskink. What you think of above question? – ADM Jan 17 '18 at 07:10
  • well i think if the OP wants his device to be in standby mode all the time he will kill his batteries very soon ;-( but yes, i agree such background tasks shouldnt be done in foreground activities – pskink Jan 17 '18 at 07:14
  • @pskink Thank you for your help!. I know of the battery drain problem. But I still have to do it. Actually it's a Device Owner App and made for work. But I need a solution for it. – Antonio Vlasic Jan 17 '18 at 07:19
  • so use `WakeLock`, it will prevent your device from sleeping, whats your current code? – pskink Jan 17 '18 at 07:21
  • @pskink Added a second edit, you can check it out – Antonio Vlasic Jan 17 '18 at 07:37
  • @pskink If you say "prevent from sleeping": Does that mean it does not get locked by itself (the device) or the cpu runs its work in the standby mode on? – Antonio Vlasic Jan 17 '18 at 07:40
  • if you dont use any wake locks your device will sleep when you press the power button, to prevent it from sleeping the wake locks have to be acquired – pskink Jan 17 '18 at 07:46
  • ok do you have repeating `TimerTask`? or you call `schedule()` inside `TimerTask#run` method to kick yourself? did you try to use `HandlerThread` instead? – pskink Jan 17 '18 at 07:47
  • @pskink I'm calling schedule inside TimerTask. I have not tried HandlerThread, what's the difference? – Antonio Vlasic Jan 17 '18 at 07:54
  • `HandlerThread` is more "android-ish" as it uses the `Looper` / `Handler` / `MessageQueue` – pskink Jan 17 '18 at 08:15

0 Answers0