title: Foreground Service goes to sleep when screen is locked
body: <p>I wish to sync the user's location to the server. I've written a foreground service for this. However when the screen is locked, the service stops sending any updates. How do I fix this?</p>
  <pre><code>public final class LocationUpdateService extends Service
  {
  private static final String TAG = &quot;LocationUpdateService&quot;;
  private FusedLocationProviderClient fusedLocationProviderClient;
  private LocationRequest locationRequest;
  private LocationCallback locationCallback;
  private Util util = new Util();
  
  private static final long LOCATION_INTERVAL = 15000;
  private static final long LOCATION_MIN_DISPLACEMENT = 20;
  
  private Handler serviceHandler;
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent)
  {
      return null;
  }
  
  @Override
  public void onCreate()
  {
      /*HandlerThread handlerThread = new HandlerThread(TAG);
      handlerThread.start();
      serviceHandler = new Handler(handlerThread.getLooper());*/
  
      fusedLocationProviderClient = new FusedLocationProviderClient(getApplicationContext());
  
      locationRequest = LocationRequest.create();
      locationRequest.setInterval(LOCATION_INTERVAL);
      locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  //            locationRequest.setSmallestDisplacement(LOCATION_MIN_DISPLACEMENT);
  
      locationCallback = new LocationCallback()
      {
          @Override
          public void onLocationResult(LocationResult locationResult)
          {
              if (locationResult == null)
              {
                  return;
              }
              Location location = locationResult.getLastLocation();
              if (location != null)
              {
                  Log.d(TAG, &quot;LocationCallback: location received&quot;);
                  sendLocation(util.getToken(getApplicationContext()), new RequestLocationUpdate(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude())));
              }
              else
              {
                  Log.d(TAG, &quot;LocationCallback: null location received&quot;);
              }
          }
      };
  
      startForeground(Constants.NOTIFICATION_ID_LOCATION, getLocationNotification());
      requestLocationUpdates();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {
      return START_STICKY;
  }
  
  
  @Override
  public void onDestroy()
  {
      super.onDestroy();
      removeLocationUpdates();
      serviceHandler.removeCallbacksAndMessages(null);
      stopSelf();
  }
  
  @Override
  public void onTaskRemoved(Intent rootIntent)
  {
      Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
      restartServiceIntent.setPackage(getPackageName());
  
      PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
      AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
      alarmService.set(
              AlarmManager.ELAPSED_REALTIME,
              SystemClock.elapsedRealtime() + 1000,
              restartServicePendingIntent);
  
      super.onTaskRemoved(rootIntent);
  }
  
  private void requestLocationUpdates()
  {
      fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
  }
  
  private void removeLocationUpdates()
  {
      fusedLocationProviderClient.removeLocationUpdates(locationCallback);
  }
  
  private Notification getLocationNotification()
  {
      return new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_IMPORTANT_ID)
              .setContentTitle(&quot;Location Service&quot;)
              .setContentText(&quot;Your location is being synced to the server&quot;)
              .setSmallIcon(R.drawable.vector_logo)
              .build();
  }
  
  private void sendLocation(String authorization, RequestLocationUpdate request)
  {
      APIClient.getClient().updateLocation(authorization, request).enqueue(new Callback&lt;ResponseGenericUser&gt;()
      {
          @Override
          public void onResponse(Call&lt;ResponseGenericUser&gt; call, Response&lt;ResponseGenericUser&gt; response)
          {
              if (response.isSuccessful())
              {
                  if (response.body().getStatus())
                  {
                      Log.d(TAG, &quot;sendLocation: Location sent successfully&quot;);
                  } else
                  {
  
                      Log.d(TAG, &quot;sendLocation: &quot; + response.body().getMessage());
                  }
              } else
              {
                  Log.d(TAG, &quot;sendLocation: &quot; + response.message());
              }
          }
  
          @Override
          public void onFailure(Call&lt;ResponseGenericUser&gt; call, Throwable t)
          {
              Log.d(TAG, &quot;sendLocation: &quot; + t.getMessage());
          }
      });
  }
  </code></pre>
  <p>}</p>