Reputation:
I've wrote an app that send gps locations every 2 minutes. The app is working fine but needs to be running and can't be on background or closed.
Is possible to switch this behavior and keep sending the gps location if the app is running on background or closed?
I've read about BroadcastReceivers but I'm not sure if this is the way to go.
Any help is appreciated.
Upvotes: 1
Views: 1521
Reputation: 1006799
Is possible to switch this behavior and keep sending the gps location if the app is running on background or closed?
Definitely not on Android 8.0+, as you will not get fresh GPS fixes very often when the device is asleep.
It is also impossible in general, as the user can stop your app whenever the user wants to, and Android can terminate your process whenever it feels that this is necessary. Plus, on Android 6.0+, Doze mode and app standby will prevent your app from doing work very often when the device is asleep.
A foreground service on the battery optimization whitelist and a continuous partial wakelock is the closest that you will get to being able to get to doing what you want. This will drain the battery very quickly, which will tend to make users rather angry.
Upvotes: 3