Reputation: 59
I have an application with service that is using Location Manager to send users location among the other data to our backend server. The service is only active if a user opens certain activities (theoretically it may never be started, but that's not the case in practice).
Now the client came with a demand that the application should send the users location at 9 AM and 5 PM.
I have used the alarm manager with the broadcast receiver to perform some tasks on specific time intervals in the past, but I'm not quite sure which scenario is best for this. The problem is that it is possible that application doesn't have the correct location yet (GPS doesn't have a fix yet), now, in that case, I should repeat the check until the location is fixed and send it then (after 9 am). Is there a better solution? And what scenario should I use for this?
Upvotes: 0
Views: 1363
Reputation: 674
All your issues stem from the fact you want to achieve everything in the background. If you can consider a foreground service (creates a UI notification) then all of the limitations I discuss below aren't applying and you have an easy life :)
Read about "App Standby" here:
When the user plugs the device into a power supply, the system releases apps from the standby state, allowing them to freely access the network and to execute any pending jobs and syncs. If the device is idle for long periods of time, the system allows idle apps network access around once a day.
This means you can not guarantee a network operation at specified time, unless you are using FCM (Firebase Cloud Messaging). BTW it's free for small volumes.
The proper way to ask for networking operation for devices >= M (Android 6) is by scheduling such tasks with JobScheduler or WorkManager. As mentioned below - you will get a wake up with networking enabled at least once a day. You just don't know when :)
Adding the location requirement makes it all a bit more tricky, since you can only get few locations per hour starting from Android O (Android 8). So you can do a quick GPS request in the background, but can not guarantee a good fix simply since you may have finished your locations count quota ¯\_(ツ)_/¯
Bottom line: either adjust your employer's expectations or consider a foreground app scenario.
Upvotes: 2
Reputation: 756
Upvotes: 0