我如何使PendingIntent触发我的GeofenceBroadcastReceirver?

问题描述 投票:0回答:1

因此,我在应用程序中使用了地理栅栏。我在GoogleMaps地图上有树的位置,所有三个都是地理围栏。

到目前为止,它们已成功构建并添加到我的应用程序中,但是当我进入地理围栏区域时,没有激活任何触发器。

位置权限正在工作,所以我认为这对他们来说不是问题,因此我不会发布它们。

我正在遵循android文档中的官方教程-link

为什么触发器不起作用?

这是我的代码:

OnCreate()

geofencingClient = LocationServices.getGeofencingClient(this)

        GeofencingConstants.BookPlaces_DATA.forEach {
            geofenceList.add(Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(it.name)

                // Set the circular region of this geofence.
                .setCircularRegion(
                    it.latLng!!.latitude,
                    it.latLng!!.longitude,
                    GeofencingConstants.GEOFENCE_RADIUS_IN_METERS
                )

                // Set the expiration duration of the geofence. This geofence gets automatically
                // removed after this period of time.
                .setExpirationDuration(GeofencingConstants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)

                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)

                // Create the geofence.
                .build())
        }

请求

     private fun getGeofencingRequest(): GeofencingRequest {
            return GeofencingRequest.Builder().apply {
                setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                addGeofences(geofenceList)
            }.build()
        }

Intent ..不触发接收器

      private val geofencePendingIntent: PendingIntent by lazy {
            val intent = Intent(this, GeofenceBroadcastReceiver::class.java)
            // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
            // addGeofences() and removeGeofences().
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        }

添加地理围栏

     private fun addGeofencesToClient(){
            geofencingClient?.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
                addOnSuccessListener {
                    // Geofences added
                    // ...
                    Log.i("added","GEOFENCES ADDED")
                    Toast.makeText(this@MapsActivity, R.string.geofences_added,
                        Toast.LENGTH_SHORT)
                        .show()
                }
                addOnFailureListener {
                    // Failed to add geofences
                    // ...
                    println(it.localizedMessage)
                    Log.i("failed","NOT ADDED")
                }
            }
        }
android kotlin android-geofence
1个回答
0
投票

它实际上正在工作。我需要做的是在模拟器上打开Goog​​leMaps,因为显然需要这样做才能获取您的位置。

© www.soinside.com 2019 - 2024. All rights reserved.