0

I have an app that uses Google Maps geofencing. (Whenever the user crosses into a fence, it triggers a PendingIntent which handles the event)

The issue is, I can't seem to get my app to update location in the background.

My current code looks like this

protected void createLocationRequest() {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "user permissions have blocked the app from getting location updates", Toast.LENGTH_SHORT).show();
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

In my activity, with the change listener.

@Override
    public void onLocationChanged(Location location) {
        Toast.makeText(this, "testing message, at location " + location, Toast.LENGTH_SHORT).show();
    }

I'm getting location updates while the app is in the foreground, as evident by OnLocationChanged and my geofences both being triggered correctly. But when the app is in the background, my geofences aren't triggered until I enter the app.

I don't even need a callback, just for the phone to check it's current location.

Any way to get location updates in the background as well?

Yonah Karp
  • 581
  • 7
  • 22
  • Possible duplicate of [Get GPS location via a service in Android](https://stackoverflow.com/questions/8828639/get-gps-location-via-a-service-in-android) – Josh Lee Sep 04 '19 at 00:20
  • **Possible duplicates** Quick answer(In office right now) 1. [Best way to get user GPS location in background in android](https://stackoverflow.com/questions/28535703/best-way-to-get-user-gps-location-in-background-in-android) 2. [Continuous location updates in background](https://stackoverflow.com/questions/21726670/continuous-location-updates-in-background) I am currently looking into the code and will update this answer if I get through. – chinmish Jun 18 '17 at 18:58
  • The code at 1 seems to constantly be checking for location (even when I changed the interval to be 30 seconds). That would be a massive battery drain :/ – Yonah Karp Jun 18 '17 at 19:07

0 Answers0