title: WorkManager stops scheduling periodic Worker after the app closes
body: <p>maybe this is yet another question about WorkManager, but I really can't find a solution...</p>
  
  <p>I'm trying, as the title suggests, to run a periodic work every 15 minutes. Actually in the worker I'm polling some data every minute. After every poll, every almost 1 second I check if the worker is being stopped and if so return, otherwise keep waiting until 1 minute is reached and poll data again.</p>
  
  <p>According to the documentation this should work and indeed it is, until I kill the app from the recent app screen.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package com.qsea.app.cordova;
  
  import android.content.Context;
  
  import android.os.SystemClock;
  
  import android.util.Log;
  
  import androidx.work.Worker;
  import androidx.work.WorkerParameters;
  
  public class ServerListenerWorker extends Worker {
  
      public static final String TAG = "ServerListenerWorker";
  
      public ServerListenerWorker(
          Context appContext,
          WorkerParameters workerParams
      ) {
          super(appContext, workerParams);
      }
  
      @Override
      public Result doWork() {
  
          Log.d(TAG, "Doing work");
  
          final long startTime = SystemClock.elapsedRealtime();
          final int maxDelta = 840000; // 14 minutes
  
          while (true) {
              // I did this to stop this worker until 15 minutes
              // and then let the next worker run
              if (SystemClock.elapsedRealtime() - startTime &gt;= maxDelta) {
                  break;
              }
  
              // Here I'm polling data, if the polling results in a failure
              // I return Result.retry()
  
              // It avoid waiting if it remains only 1 minute until the max time
              if (SystemClock.elapsedRealtime() - startTime &gt;= (maxDelta - 60000)) {
                  break;
              }
              for (int i = 0; i &lt; 60; i++) {
                  SystemClock.sleep(950);
                  // Here it checks if it is stopped
                  if (isStopped()) {
                      Log.d(TAG, "Detected stop"); // this is actually never reached
                      break;
                  }
              }
          }
          return Result.success();
      }
  }
  </code></pre>
  
  <p>And I do as following to start the work</p>
  
  <pre><code>Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
  PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(ServerListenerWorker.class, 15, TimeUnit.MINUTES)
      .addTag(serverListenerWorkerUID)
      .setConstraints(constraints)
      .build();
  WorkManager.getInstance(cordova.getActivity()).enqueueUniquePeriodicWork(serverListenerWorkerUID, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
  </code></pre>
  
  <p>I check the correct working of the worker by viewing the log of my server (If I receive the request it is working fine) and it seems to work ok until I close the app. Then it stops running the actual worker and never run a worker again until I reopen the app, where the work seems to be enqueued and resumes right after the app opening.</p>
  
  <p>Am I doing something wrong in the initialization?
  I have also <code>&lt;service android:name=".ServerListenerWorker" android:permission="android.permission.BIND_JOB_SERVICE" /&gt;</code> in my AndroidManifest.xml</p>
  
  <p>Is this expected behavior?</p>
  
  <p>I read that chinese ROM have additional restriction to background services and I have a HUAWEI, which seems to be the worst in this. So could it be the device? And if so, how do Facebook, WhatsApp, Instagram and others manage this to work even in these devices?</p>