Hello guys, Have anyone used geofencing? I am tryi...
# android
r
Hello guys, Have anyone used geofencing? I am trying to use it and I am stuck..
n
https://mindorks.com/android/store/Location/patloew/rxlocation just refer this see if it might help to solve your problem
One more blog for your problem
c
I have used it what are you stuck on? @Remon Shehata
r
@codeslubber can I share with you my code?
this is my Geofence class
Copy code
package iti.intake40.mawgood.core

import android.app.Activity
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices

class GeoFenceHelper {
    lateinit var geofencingClient: GeofencingClient
    lateinit var geofencingRequest: GeofencingRequest


    fun build(activity: Activity, latitude: Double, longitude: Double, radiusInMeters: Float) {
        geofencingClient = LocationServices.getGeofencingClient(activity)

        //build the geofence
        val fence = Geofence.Builder()
            .setRequestId("123")
            .setCircularRegion(latitude, longitude, radiusInMeters)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .build()

        geofencingRequest = GeofencingRequest.Builder().apply {
            setInitialTrigger(
                GeofencingRequest.INITIAL_TRIGGER_ENTER
                        or GeofencingRequest.INITIAL_TRIGGER_DWELL
                        or GeofencingRequest.INITIAL_TRIGGER_EXIT
            )
            addGeofence(fence)
        }.build()


        //build pending intent
        val geofencePendingIntent: PendingIntent by lazy {
            val intent = Intent(activity, GeofenceReceiver::class.java)
            PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        }

        //request
        geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run {
            addOnFailureListener {
                // display error

                Log.d("GeofenceReceiver", "helper onFailure")
            }
            addOnSuccessListener {
                // move on
                Log.d("GeofenceReceiver", "helper onsuccess")
                //reg broadcast
                val intentFilter = IntentFilter("locationAction")
                val geofenceReceiver = GeofenceReceiver()
                activity.registerReceiver(geofenceReceiver, intentFilter)

                //send broadcast intent
                val broadcastIntent = Intent()
                broadcastIntent.setAction("locationAction")
                activity.sendBroadcast(broadcastIntent)
                //unreg the broadcast receiver
                activity.unregisterReceiver(geofenceReceiver);

            }
        }
    }


}
Copy code
override fun onStart() {
    super.onStart()
    LocationPermessions(this, this).requestForegroundAndBackgroundLocationPermissions()
    GeoFenceHelper().build(this, 30.621166, 32.268710, 5000f)

}
it jumps to this part
Copy code
geofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run
and it enter add on success listener.
but it never enter the broadcast reciever
@Nikhil Mule thanks bro. I am reading the links now
found the problem..thanks a lot guys
c
What was the problem @Remon Shehata?
r
turned out that the code was right. the problem was I used to fake my GPS location using set location in the emulator settings. I installed fake GPS app from play store and used it
👍🏻 1
c
Yeah the emulator location simulation sucks.
👍 1
r
I agree.. Even though I read on androids.developer that I can use it