TheJadav

Add geofences in android

Geofences is a virtual circle created around specified location with specified radius. It is the awareness of users current location with his proximity location. If you want to notify user for particular location you can do this with Geofence. Get location details for that location and add geofence with certain radius. You can listen enter, exit and wait ( or dwell) event for that location and based on that you can notify user

Create Geofence client and Add

private var geofenceList: ArrayList<geofence> = ArrayList()
geofencingClient = LocationServices.getGeofencingClient(this)

Create list for geofences

private var geofenceList: ArrayList<geofence> = ArrayList()
geofenceList.add(Geofence.Builder().setRequestId(requestId.toString()).setCircularRegion(latitude, longitude, distance)
                        .setLoiteringDelay(60000).setExpirationDuration(-1).setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL or Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT).build())

you can check all parametersĀ here

Create Geofences request

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

Create pending Intent for Geofences

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

You need to create one Intent Service to receive events from Geofence. In this MyGeoFencingTransition is that one. You must need to register this service in Manifest.xml file.

Add geofences now

geofencingClient.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
            addOnSuccessListener {
                Toast.makeText(baseContext, "Geofence successfully added", Toast.LENGTH_SHORT).show()
            }

            addOnFailureListener {
                Toast.makeText(baseContext, "Geofence exception occurs -->  ${it.localizedMessage}", Toast.LENGTH_SHORT).show()
            }
        }

And the final one MyGeoFencingTransition

class MyGeoFencingTransition : IntentService("MyGeoFencingTransition") {
    override fun onHandleIntent(intent: Intent?) {
        val geofencingEvent = GeofencingEvent.fromIntent(intent)
        if (geofencingEvent.hasError()) {
//            Log.e("Geofence", geofencingEvent.errorCode.toString())
            return
        }

        // Get the transition type.
        val geofenceTransition = geofencingEvent.geofenceTransition
        sendNotification(geofencingEvent.triggeringGeofences, geofenceTransition, geofencingEvent.triggeringLocation)

    }

    private fun sendNotification(triggeringGeofences: List<geofence>, geofenceTransition: Int, triggeringLocation: Location) {

        //You can receive this broadcast by creating broadcast receiver in activity.
        val geofenceIds: ArrayList<string> = ArrayList()
        triggeringGeofences.forEach {
            geofenceIds.add(it.requestId)
        }
        val intent = Intent("$packageName.GeofenceTriggered")
        intent.putExtra(ParamsUtils.geofenceIds, geofenceIds) //geofence Ids for which event occurs
        intent.putExtra(ParamsUtils.geofenceTransition, geofenceTransition) // geofence event can be Geofence.GEOFENCE_TRANSITION_ENTER || Geofence.GEOFENCE_TRANSITION_EXIT || Geofence.GEOFENCE_TRANSITION_DWELL
        intent.putExtra(ParamsUtils.latitude, triggeringLocation)
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
    }
}

Now you can receive broadcast in activity and perform any task. Please always checkĀ best practices

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message