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?