Geofences do not work as expected

后端 未结 1 1097
执念已碎
执念已碎 2021-01-15 20:50

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 elsewh

相关标签:
1条回答
  • 2021-01-15 21:20

    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.

    0 讨论(0)
提交回复
热议问题