Remon Shehata
05/12/2020, 9:55 PMNikhil Mule
05/12/2020, 10:08 PMNikhil Mule
05/12/2020, 10:14 PMNikhil Mule
05/12/2020, 10:14 PMcodeslubber
05/12/2020, 11:07 PMRemon Shehata
05/12/2020, 11:16 PMRemon Shehata
05/12/2020, 11:23 PMpackage 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);
}
}
}
}
Remon Shehata
05/12/2020, 11:23 PMoverride fun onStart() {
super.onStart()
LocationPermessions(this, this).requestForegroundAndBackgroundLocationPermissions()
GeoFenceHelper().build(this, 30.621166, 32.268710, 5000f)
}
Remon Shehata
05/12/2020, 11:24 PMgeofencingClient.addGeofences(geofencingRequest, geofencePendingIntent).run
and it enter add on success listener.Remon Shehata
05/12/2020, 11:24 PMRemon Shehata
05/12/2020, 11:25 PMRemon Shehata
05/13/2020, 12:35 AMcodeslubber
05/13/2020, 2:36 AMRemon Shehata
05/13/2020, 8:08 AMcodeslubber
05/13/2020, 2:33 PMRemon Shehata
05/13/2020, 2:37 PM