Geofences do not work as expected

最后都变了- 提交于 2019-12-01 13:18:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!