title: How to keep alive task into Foreground Service in standby / Doze mode?
body: <p>I have a foreground service that manages some timer counters based on each 1s from a task. But, in <strong>standby mode</strong> or <strong>doze mode</strong>, the task is switch to pause by the system. This behavior is not present on all the mobiles, but on Huawei, Xiaomi is very often. </p>
  
  <p>Have you got some ideas guys to fix this trouble?</p>
  
  <p>Thank you very much</p>
  
  <p>My service:</p>
  
  <pre><code>public class MyTimerService extends Service {
  
  @SuppressLint("WakelockTimeout")
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      if (intent == null) return START_STICKY_COMPATIBILITY;
  
      // First at all, generate system notification with foreground link
      createNotification();
  
      ...
  
      mExecutorFuture = MyApplication.getInstance().getExecutorServiceScheduler().
                  scheduleWithFixedDelay(() -&gt; {
  
                      lockCPU()
  
                      ...
  
                      unlockCPU()
  
                      }
                  }, 0, 1/* every 1 second*/, TimeUnit.SECONDS);
      }
  
      return START_STICKY;
  }
  
  private void lockCPU() {
      PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
      mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
      mWakeLock.acquire();
  }
  
  @Override
  public void onDestroy() {
      resetSound();
  
      super.onDestroy();
  
      ...
  
      if (mExecutorFuture != null) {
          mExecutorFuture.cancel(true);
      }
  }
  
  private void unlockCPU() {
      if (mWakeLock != null &amp;&amp; mWakeLock.isHeld()) {
          mWakeLock.release();
          mWakeLock = null;
      }
  }
  </code></pre>