10

I followed this tutorial: https://developer.android.com/training/location/geofencing and works fine on Android < 8, but in Oreo i have problems due to new OS background limitations.

How can I get geofence transition triggers when app is in background?

I also tried to use a BroadcastReceiver instead of IntentService, but the result is the same.

Pending Intent:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
    intent.action = "com.example.GEOFENCE_TRANSITION"
    PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Register geofence:

geofencingClient.addGeofences(request, geofencePendingIntent).run {
    addOnSuccessListener {
        Log.d(TAG, "Geofence added")
    }
    addOnFailureListener {
        Log.e(TAG, "Failed to create geofence")
    }
} 

Broadcast Receiver:

class GeofenceBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        Log.d(TAG, "onReceive")
    }
}

Receiver in Manifest:

<receiver android:name=".GeofenceBroadcastReceiver">
    <intent-filter>
        <action android:name="com.example.GEOFENCE_TRANSITION"/>
    </intent-filter>
</receiver>

Thanks

EDIT: IntentService version

Pending Intent:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceIntentService::class.java)
    PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Intent Service:

class GeofenceIntentService : IntentService("GeofenceIntentService") {
    override fun onHandleIntent(p0: Intent?) {
        Log.d(TAG, "onHandleIntent")
    }
}

Service in Manifest:

<service android:name=".GeofenceIntentService"/>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Federico Matera
  • 235
  • 2
  • 7

2 Answers2

1

You should get an Intent every couple of minutes on Android 8 when your geofence transition is reached in background.

See: https://developer.android.com/training/location/geofencing#java

Handle geofence transitions When Location Services detects that the user has entered or exited a geofence, it sends out the Intent contained in the PendingIntent you included in the request to add geofences. This Intent is received by a service like GeofenceTransitionsIntentService, which obtains the geofencing event from the intent, determines the type of Geofence transition(s), and determines which of the defined geofences was triggered. It then sends a notification as the output.

Note: On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes. To learn how to adapt your app to these response limits, see Background Location Limits.

Once the geofence service is registered it´s still there and you have nothing else to do and only check your IntentService for the specific PendingIntent, exclude when the device is rebooted you need to reregister your geofence service.

Also check: https://developer.android.com/about/versions/oreo/background-location-limits

edhair
  • 195
  • 1
  • 15
  • Keep in mind that there are several situations in which you'll need to re-register geofences. Please see [my answer to a related question](https://stackoverflow.com/questions/44323848/geofence-triggering-procedure-explanation-needed/50864092#50864092) for more information. – Michael Krause Jul 17 '18 at 19:53
-1

i use dexter library for permission geofence and this work for android 8 9 10 and above you must add background permission

 Dexter.withActivity(this@Activity_Map)
            .withPermissions(
                    Manifest.permission.ACCESS_COARSE_LOCATION
                    ,Manifest.permission.ACCESS_FINE_LOCATION
                    ,Manifest.permission.ACCESS_BACKGROUND_LOCATION
            )
            .withListener(object: MultiplePermissionsListener {
                override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                    report?.let {
                        if(report.areAllPermissionsGranted()){
                  //put your code here
                            }
                }
reza rahmad
  • 1,009
  • 10
  • 16
  • Even adding the permission, the application can read location without haveing foreground location service, like open Google Maps application. – Zaher88abd Apr 10 '21 at 18:15