Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android地理围栏退出触发器_Android_Firebase_Android Geofence - Fatal编程技术网

Android地理围栏退出触发器

Android地理围栏退出触发器,android,firebase,android-geofence,Android,Firebase,Android Geofence,我一直在尝试在我的应用程序中实现一个地理围栏来启动某项服务,虽然enter触发器工作,但我似乎无法让exit触发器也工作 我正在从firebase读取lat long radius,以便于访问和调试。这是我的代码: private val geofencePendingIntent: PendingIntent by lazy { val intent = Intent(this, GeofenceBroadcastReceiver::class.java) intent.act

我一直在尝试在我的应用程序中实现一个地理围栏来启动某项服务,虽然enter触发器工作,但我似乎无法让exit触发器也工作

我正在从firebase读取lat long radius,以便于访问和调试。这是我的代码:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(this, GeofenceBroadcastReceiver::class.java)
    intent.action = GEOFENCE
    PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun addGeoFence() {
    val geoFenceRequest = getGeoFencingRequest()
    checkOrAskLocationPermission {
        geofencingClient.addGeofences(geoFenceRequest, geofencePendingIntent)
            .addOnSuccessListener {
                Log.d(TAG, "GeoFence Added")
            }.addOnFailureListener {
                Log.d(TAG, "GeoFence Failed to Add")
            }
    }
}

private fun getGeoFencingRequest(): GeofencingRequest {
    val geoFence = Geofence.Builder()
        .setRequestId(GEO_FENCING_REQUEST_CODE)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setCircularRegion(
            lat,
            lng,
            radius
        )
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
        .build()

    val geoFenceList: MutableList<Geofence> = mutableListOf(geoFence)

    return GeofencingRequest.Builder().apply {
        setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER or GeofencingRequest.INITIAL_TRIGGER_EXIT)
        addGeofences(geoFenceList)
    }.build()
}
为什么退出转换从未触发

另外,我在firebase上有我的lat long,当我第一次用radius 100设置它们时,我看到enter触发器,但即使我将radius设置为1,它也不会触发出口,或者我必须用设备本身移动很远吗

class GeofenceBroadcastReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
    val geofencingEvent = GeofencingEvent.fromIntent(intent)
    val geofenceTransition = geofencingEvent.geofenceTransition

    context?.let {
        if (intent?.action == Constants.GEOFENCE)
            if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                //Start 
            } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
                //Stop 
            }
    }
}

}