Geofences in earlier versions of Android work just fine, but in Android 11(*) it doesn't seem to work as-is.
So I made a marathon post where I did a lot of tests, trying to figure out things:
It seems the root problem is that the OS kill the app to save battery. In itself this shouldn't be a problem at all, as long as the OS launch the app when it detects a geofence event.
The problem is that the OS doesn't do this. This seems super strange to me, and I have a very strong feeling of me missing something important. In the meantime, I found two workarounds of keeping the app alive, none of them are groundbreaking for forum members. None of these seem optimal, and I want to repeat that I suspect none of these are the intended solutions for working geofences in Android 11. It's just the best I've been able to figure out:
1. Create a basic foreground service
Pros
Cons
2a. Disable battery optimization, method 1: Asking user to do it manually
This method is just guiding the user to do it for us.
Pros
Cons
2b. Disable battery optimization, method 2: Using manifest and simple user action
This method consists of adding something to the manifest, and then use an intent that pretty much ask the user "should we disable battery optimization for this app?" with a simple Yes/No for the user. (Note: I haven't researched the details more than this, but I don't think I'm far away from how it's working.)
Pros
Cons
For what it's worth, I'm certain that I for my app do have a solid use-case for 2b. But I still don't have a reliable way of discussing things with Google so I am uncertain I dare to try that method.
Questions
(*) I don't know exactly what version of Android it became more problematic, perhaps 10?
So I made a marathon post where I did a lot of tests, trying to figure out things:
[SOLVED] Problem getting geofences to work in later versions of Android
UPDATE: I added Test 4, 5, 6, 7, 8, 9, 10 and 11 to this post. I've also posted a new thread with a follow-up. This post is entirely based based on the code in https://www.b4x.com/android/forum/threads/geofence-monitoring-a-region-in-the-background.84767/ I'm trying to get geofences to work on...
www.b4x.com
It seems the root problem is that the OS kill the app to save battery. In itself this shouldn't be a problem at all, as long as the OS launch the app when it detects a geofence event.
The problem is that the OS doesn't do this. This seems super strange to me, and I have a very strong feeling of me missing something important. In the meantime, I found two workarounds of keeping the app alive, none of them are groundbreaking for forum members. None of these seem optimal, and I want to repeat that I suspect none of these are the intended solutions for working geofences in Android 11. It's just the best I've been able to figure out:
1. Create a basic foreground service
Pros
- Doesn't require any action by the user
Cons
- Add to visual clutter in phone (while likely not adding much useful information)
- Uncertain it actually helps keep the app alive in the long run? (opinions welcome!)
2a. Disable battery optimization, method 1: Asking user to do it manually
This method is just guiding the user to do it for us.
Pros
- Guaranteed to work, if we can get the user to do it
Cons
- Different versions of OS and phone model might require different ways for user to navigate the OS user interface - difficult to give simple instruction that applies to everybody?
- Complicated instructions for the typical user. Example how other people have solved it: (source)
- Click DISABLE in our custom popup (where we mention that disabling battery optimization will give them more consistent location tracking experience)
- Select 'All Apps' from Battery Optimization Android Settings page
- Search and select our app name
- Select 'Don't Optimize'.
2b. Disable battery optimization, method 2: Using manifest and simple user action
This method consists of adding something to the manifest, and then use an intent that pretty much ask the user "should we disable battery optimization for this app?" with a simple Yes/No for the user. (Note: I haven't researched the details more than this, but I don't think I'm far away from how it's working.)
Pros
- Guaranteed to work
Cons
- Very dangerous, Google might ban app (source, source 2, source 3) if you can't give an acceptable use-case
- No obvious way of communicating with Google about this, either before submitting app, before getting banned, or after getting banned
For what it's worth, I'm certain that I for my app do have a solid use-case for 2b. But I still don't have a reliable way of discussing things with Google so I am uncertain I dare to try that method.
Questions
- Did I miss any pros or cons?
- Are all of these methods roughly the same from a battery-usage perspective? Anyone better or worse?
- What are your opinions on the best strategy?
(*) I don't know exactly what version of Android it became more problematic, perhaps 10?
Last edited: