Android 如何删除所有活动的地理围栏?

Android 如何删除所有活动的地理围栏?,android,android-geofence,Android,Android Geofence,在开发过程中,我一直在使用Geofences进行测试,最初添加了许多Geofences,这些Geofences将.setExpirationDuration()设置为不确定(永不过期)。他们在我的广播接收器中执行了一些代码,但我从未删除实际的地理围栏,我也没有他们的requestid 如何检查这些地理围栏是否仍处于活动状态?我想摆脱它们,以防止它们在记忆中积累 代码基本上与官方Android教程相同: 代码: 你不能。谷歌没有提供API来检查哪些GeoFence处于活动状态。你所能做的就是在你这

在开发过程中,我一直在使用Geofences进行测试,最初添加了许多Geofences,这些Geofences将
.setExpirationDuration()
设置为不确定(
永不过期
)。他们在我的广播接收器中执行了一些代码,但我从未删除实际的地理围栏,我也没有他们的
requestid

如何检查这些地理围栏是否仍处于活动状态?我想摆脱它们,以防止它们在记忆中积累

代码基本上与官方Android教程相同:

代码:


你不能。谷歌没有提供API来检查哪些GeoFence处于活动状态。你所能做的就是在你这边保留一个活动的地理围栏列表(sqlite或其他什么),并在需要时停用它们

private fun createOnlineGeofence(currentLocation: GeoPoint){
    val geofenceId = randomAlphanumeric(10, 12)
    with(sharedPref.edit()) {
        putString("geofenceId", geofenceId)
        apply()
    }
    user.geofence = Geofence.Builder().apply {
        setRequestId(randomAlphanumeric(10, 12))
            setCircularRegion(currentLocation.latitude, currentLocation.longitude, 400f)
            setExpirationDuration(28800000)
            setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT or Geofence.GEOFENCE_TRANSITION_ENTER)
            setNotificationResponsiveness(300000)
    }.build()
    val geofenceRequest = GeofencingRequest.Builder().apply {
        setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
        addGeofence(user.geofence)
    }.build()
    val onlineGeofencePendingIntent: PendingIntent by lazy {
        val intent = Intent(mContext.applicationContext, GeofenceBroadcastReceiver::class.java)
        intent.putExtra("geofenceId", geofenceId)
        PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }
    Log.d(TAG, "Created online Geofence, geofenceId: ${user.geofence?.requestId}")
    if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        geofencingClient.addGeofences(geofenceRequest, onlineGeofencePendingIntent)?.run {
            addOnSuccessListener {
                Toast.makeText(mContext, "Online Geofence added", Toast.LENGTH_LONG).show()
                Log.d(TAG, "Online geofence added")
            }
            addOnFailureListener {
                exception -> Log.d(TAG, "Exception: $exception")
            }
        }
    }
}