Как заставить PendingIntent инициировать мой GeofenceBroadcastReceirver? - PullRequest
0 голосов
/ 12 марта 2020

Итак, я использую геозоны в своем приложении. У меня есть древовидные местоположения на карте GoogleMaps, и все три являются геозонами.

Пока они успешно построены и добавлены в мое приложение, но при входе в область геозон нет триггера.

Разрешения на расположение работают, поэтому я предполагаю, что это не проблема, поэтому я не публикую их.

Я следую официальному руководству из android документации - ссылка

Почему триггер не работает?

Вот мой код:

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()
        }

Намерение .. делает не активировать приемник

      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")
                }
            }
        }

1 Ответ

0 голосов
/ 12 марта 2020

Это на самом деле работает. Что мне нужно было сделать, это открыть GoogleMaps на эмуляторе, потому что, очевидно, это нужно, чтобы узнать ваше местоположение.

...