title: Android location service stop working when app destroyed or goes in background
body: <p>I am building a location based application and I have created a service which works in background. When app goes in background or destroyed  and in my service class if I play audio in loop it works perfectly.</p>
  
  <p>Audio is playing continue even I destroy app and for getting user location I am using <strong>FusedLocationProviderClient</strong> API and it also works from service but only when app is in foreground when app user goes to other app or destroys app this it's not getting location but media player is working fine in same service.</p>
  
  <p><strong>My Service Class</strong></p>
  
  <pre><code>public class ServiceClass extends Service {
  
      private MediaPlayer mediaplayer;
      private FusedLocationProviderClient fusedLocationProviderClient;
      private LocationRequest locationRequest;
      private Context mContext;
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          //Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
          mContext = this;
          mediaplayer = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI);
          mediaplayer.setLooping(true);
          mediaplayer.start();
          getLocation();
  
          return START_STICKY;
      }
  
      public void getLocation() {
  
          Toast.makeText(mContext, "Engine X started YO", Toast.LENGTH_SHORT).show();
  
          fusedLocationProviderClient = new FusedLocationProviderClient(this);
          locationRequest = new LocationRequest();
          locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
          locationRequest.setFastestInterval(1000);
          locationRequest.setInterval(1000);
  
          fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback(){
  
              @Override
              public void onLocationResult(LocationResult locationResult) {
                  super.onLocationResult(locationResult);
                   Log.d("X_loc_BAck", ""+locationResult.getLastLocation().getAccuracy());
                  Toast.makeText(mContext, ""+locationResult.getLastLocation().getLatitude(), Toast.LENGTH_SHORT).show();
  
  
  
              }
          },getMainLooper());
  
      }
  
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          mediaplayer.stop();
          //Toast.makeText(this, "Service Desttroyed", Toast.LENGTH_SHORT).show();
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
          super.onTaskRemoved(rootIntent);
          mediaplayer.stop();
          Intent i = new Intent(this, ServiceStopped.class);
          sendBroadcast(i);
          //Toast.makeText(this, "Service Task Removed", Toast.LENGTH_SHORT).show();
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  }
  </code></pre>