Creating and Monitoring Geofences example app on github does not work as expected, Geofences in Google Play Services don't seem to work properly.
I have read elsewhere at Stack Overflow, that Reto Meier mentioned at Google IO that the GPS will turn on only if the phone approaches the geofence to conserve battery. Seems that it does not turn on at all.
Here is the situation:
- I have an Emulator with Google APIs (same happens with real phone).
- I have the above mentioned app out of the box installed and Geofence added.
- I have a KML file that drives me in and out of the Geofence.
Gofence ENTER is not triggered.
Now, just to be sure that KML actually works, I open Google maps. I play KML and I can see the blue "You Are Here" dot nicely moving in and out of Geofence, and the geofences do get triggered.
While Google Maps is up and running, I see GPS indicator in status bar.
So, it seems to me that Goefence only gets triggered if the GPS is explicitly on.
Can somebody explain this please? There is a lot of questions here on Stack Overflow that deal with the same problem.
Thanks.
EDIT: Did the field test with real phone. While Location Updates app was running and tracking me Creating and Monitoring Geofences example app was getting ENTER/EXIT events. As soon as I killed Location Updates app, the Geofences app stopped getting ENTER/EXIT events.
Seems to me like Google has optimized the power consumption for Geofences to the point of uselessness.
I found the solution. It's an abuse, but that's the only way to get Geofence useful.
All I need to do is request location updates:
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, mGeofencePendingIntent);
And now we can register geofences.
Here is my understanding why the above works: According to documentation: "If your application wants to passively observe location updates triggered by other applications, but not consume any additional power otherwise, then use the PASSIVE_PROVIDER This provider does not actively turn on or modify active location providers"
Seems to me that added geofence is one of PASSIVE_PROVIDERS. That would explain why geofence got triggered when Google Maps was open.
Of course, we could use PRIORITY_BALANCED_POWER_ACCURACY instead of HIGH (~40m accuracy) to preserve some battery and be smart about when we put geofence on and for how long.
来源:https://stackoverflow.com/questions/28116521/geofences-do-not-work-as-expected