2

I am creating an app that uses Geofence monitoring, I have successfully implemented this functionality but have encountered an issue while testing it with the "Device Only" location setting which uses GPS by itself.

I can get location data with Device only but when I try to add geofences to monitor it always returns status code 1000 which is GEOFENCE_NOT_AVAILABLE.

This code below to addGeofences is called after I have confirmed Location permissions

 LocationServices.GeofencingApi.addGeofences(mApiClient, getGeofencingRequest(),
                    mGeofenceRequestIntent).setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if(!status.isSuccess()) {
                        Toast.makeText(mContext, String.format("Monitor Geofences %s", status.getStatusCode()), Toast.LENGTH_LONG).show();
                    }
                    else{
                        Toast.makeText(mContext, String.format("Successfully Monitor Geofences %s", status.getStatusCode()), Toast.LENGTH_LONG).show();
                    }
                }
            });

Am I doing something wrong or is my only option to force the user to use the Battery saving of High accuracy location modes?

Gooner
  • 387
  • 2
  • 23

1 Answers1

0

Geofencing based on google services does not request GPS updates by default. It must be 'forced' to do so, after that, your app should work again as expected.

The way it has worked for me was implementing LocationListener to the public class on the activity file that holds your GoogleAPIClient initialisation, GeofencingRequest, etc

Then in that same public class add at the end

private LocationManager locationManager;

Followed by this at the end of your onCreate method

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

As well as this to onResume ('1000' and '5' being the update rate in milliseconds and number of samples per update, respectively)

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, this);

And finally add this to onPause if you want to save battery when the app is not on foreground but you will miss all the notification that occur during this downtime by my own experience

locationManager.removeUpdates(this);

Also, remember to import all of the new packages.

If you were able to get notifications (regardless of accuracy) before changing your system settings and you didn't change anything else, I believe this might solve your issue.

I notice you mention that you're getting location data with Device Only setting enabled but perhaps only thru applications like Maps, for instance, that have the ability to request gps data from that provider even if WI-FI, CELLULAR DATA or both are disabled. You can also post some more code to help you pin-point the problem.

klein_gat
  • 83
  • 7
  • by the way, after reading a bit one of the links to the right I also realised the error number you're having has to do with changing location mode in settings as all ongoing geofences get unregistered when that happens, so in order to avoid this issue you would have to employ specific logic in your code to make up for that change, which by the time you may know already, but just in case, here it is: http://stackoverflow.com/questions/19082482/ – klein_gat Jul 04 '16 at 14:17