androidandroid-studiogoogle-maps-api-3fusedlocationproviderapi

Service is not giving Location Updates


I am trying to have an app give location updates while the app is in the background. When the app is open the service works fine. When the app is in the background the service continues running but the location updates stop. I have tried using a foreground service but this did not help.

I am using google's fusedLocationProviderClient

public int onStartCommand(Intent intent, int flags, int startId) {
      client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}

With a location request defined in onCreate()

locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(500);
locationRequest.setFastestInterval(500);

And callback defined as:

private final LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        List<Location> locationList = locationResult.getLocations();
        if (locationList.size() != 0) {
            Location location = locationList.get(0);
            Log.e("AppLocationService", "Latitude  - " +location.getLatitude()+", longitude  - " +location.getLongitude() );
        }
    }

I'm really new to android studio so any help is much appreciated!

UPDATE:

Service started through startService()

public class BackgroundService2 extends Service {
private FusedLocationProviderClient client;
private LocationRequest locationRequest;

@Override
public void onCreate() {
    super.onCreate();
    client = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(500);
    locationRequest.setFastestInterval(500);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent,flags,startId);
    try {
        if ( Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
        }

    } catch (SecurityException ignore) {
        Log.e("AppLocationService", "SecurityException - " + ignore.toString(), ignore);
    }
    return START_STICKY;
}


private final LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        List<Location> locationList = locationResult.getLocations();
        if (locationList.size() != 0) {
            Location location = locationList.get(0);
            Log.e("AppLocationService", "Latitude  - " +location.getLatitude()+", longitude  - " +location.getLongitude() );
        }
    }

};


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

}


Solution

  • Turns out it was a problem with my manifest. Not only is it necessary to declare the service, but also its type.

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    

    as well as

    <service
            android:name=".BackgroundService"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="location" />