Getting location when the app is closed using fusedLocationProviderClient

74 Views Asked by At

I am currently working on a location-based reminder app. Obviously, the should send a notification when the user is close to a location he set.

For that, I am using a foreground service - which starts when the app starts. It also works when the app is closed, like swiped up from the apps list (not force stopped).

In the service, I am getting the current location and iterating over the location to see which ones I should send a notification for.

Every thing works fine, but for some reason, when the app is closed, the service doesn't enter the onLocationResult:

LocationServices.getFusedLocationProviderClient
                        (ServiceLocationNotification.this).requestLocationUpdates(locationRequest, new LocationCallback() {
                            @Override
                            public void onLocationResult(@NonNull LocationResult locationResult) {
                                GetCurrentLocationAsyncTaskServiceLocationNotification task =
                                        new GetCurrentLocationAsyncTaskServiceLocationNotification
                                        (ServiceLocationNotification.this,
                                                locationResult,
                                                this);

                                try
                                {
                                    currentLatLng = task.execute().get();
                                }
                                catch (Exception e)
                                {
                                    System.out.println("Exception");
                                    e.printStackTrace();
                                }
                            }
                        },
                        Looper.getMainLooper()); // Use the main thread's looper to receive location updates

(I checked with prints).

GetCurrentLocationAsyncTaskServiceLocationNotification (sorry for the long name) is a class i created to retrieve the current location and get the latLng out of it:

// for the ServiceLocationNotification class.
public class GetCurrentLocationAsyncTaskServiceLocationNotification extends AsyncTask<Void, Void, LatLng>
{
    private WeakReference<ServiceLocationNotification> ServiceLocationNotificationWeakReference;
    private LocationCallback locationCallback;
    private LocationResult locationResult;

    GetCurrentLocationAsyncTaskServiceLocationNotification(ServiceLocationNotification activity,
                                              @NonNull LocationResult locationResult,
                                              LocationCallback locationCallback)
    {
        ServiceLocationNotificationWeakReference = new WeakReference<>(activity);
        this.locationResult = locationResult;
        this.locationCallback = locationCallback;
    }

    // .execute().get() gets the return value.
    @Override
    protected LatLng doInBackground(Void... voids)
    {
        double latitude;
        double longitude;

        // get location
        LocationServices.getFusedLocationProviderClient(ServiceLocationNotificationWeakReference.get())
                .removeLocationUpdates(locationCallback);

        // Check if the location result is not null and has at least one location
        if (locationResult != null && locationResult.getLocations().size() > 0)
        {
            // Get the most recent location from the location result
            int index = locationResult.getLocations().size() - 1;
            latitude = locationResult.getLocations().get(index).getLatitude();
            longitude = locationResult.getLocations().get(index).getLongitude();

            return new LatLng(latitude, longitude);
        }
        return null;
    }
}

I would appreciate any help understanding why this happens (not entering the onLocationResult function). Thanks!

0

There are 0 best solutions below