【问题标题】:How do I make the PendingIntent trigger my GeofenceBroadcastReceirver?如何使 PendingIntent 触发我的 GeofenceBroadcastReceirver?
【发布时间】:2020-03-12 12:02:30
【问题描述】:

所以,我在我的应用程序中使用了地理围栏。我在 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")
                }
            }
        }

【问题讨论】:

  • 嗨 Fiphe,您是否创建了 JobIntentService。
  • 嘿,我刚刚想通了。它实际上正在工作。我必须在模拟器上打开谷歌地图,否则它无法获取我的位置。
  • 首先你应该实现LocationListener。然后你可以使用假的位置应用程序或发送新的位置到模拟器。

标签: android kotlin android-geofence


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多