GeofencingClient需要花费大量时间来添加地理围栏

时间:2019-05-30 09:01:53

标签: android kotlin google-play-services android-geofence

我有一个依赖于GeoFencing技术的功能,存在一个奇怪的问题。添加地理围栏后,客户端会花费大量时间(查看日志)。这很奇怪,因为它很早就可以很好地工作(没有与位置和所有afaik相关的代码更改)而没有任何延迟。

我正在使用以下依赖项:

  

'com.google.android.gms:play-services-location:16.0.0'

@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    fun addGeoFences(
            geoFences: ArrayList<Geofence>,
            onSuccess: () -> Unit = {},
            onFailure: (Exception) -> Unit = {}
    ): Boolean {
        if (isAddingGeofence) {
            GlobalLoggerUtils.showLog(TAG, "isAddingGeofence: $isAddingGeofence")
            return false
        }
        GlobalLoggerUtils.showLog(TAG, "addGeoFences()")

        if (geoFences.size == 0) {
            return false
        }
        isAddingGeofence = true

        geoFencingClient
                .addGeofences(
                        getGeoFencingRequest(geoFences),
                        geoFencePendingIntent
                ).addOnSuccessListener { result ->
                    isAddingGeofence = false
                }
                .addOnFailureListener { exception ->
                    isAddingGeofence = false
                }
                .addOnCanceledListener {
                    isAddingGeofence = false
                }
        return true
    }



2019-05-30 12:43:29.716  I/: addGeoFences() 
2019-05-30 12:44:00.602  I/: isAddingGeofence: true 
2019-05-30 12:44:02.424  I/: isAddingGeofence: true 
2019-05-30 12:44:03.525  I/: isAddingGeofence: true 
2019-05-30 12:44:04.975  I/: isAddingGeofence: true 
2019-05-30 12:44:06.541  I/: isAddingGeofence: true 
2019-05-30 12:48:18.384  I/: add success >>> result: null  
2019-05-30 12:48:18.393  I/: add success >>> requestIds: XYZ  

让我知道是否有必要在上下文中添加此问题。按照official Android documentation添加地理围栏。

1 个答案:

答案 0 :(得分:0)

If it's Android 8.0+, be aware that there is a background location restriction that also applies to the Geofencing APIenter link description here