https://kotlinlang.org logo
#android
Title
# android
a

Alexander

10/20/2019, 10:41 AM
I'm new to Android programming and I have been trying to decide on the best approach for polling a device for long/lat. Originally, I was going to have this in a background service, but having read that you can only really poll once in a 15 minute time-frame I have since abandoned that. I have read that AlarmManager can be used however, I feel like this to, isn't suitable. I have been looking into Foreground Services and they seem OK - however; Whats my best approach if I want to run a task in a foreground service say every 10 seconds? - is a foreground service even the right way to go?
k

Kirill Zhukov

10/20/2019, 10:44 AM
WorkManager
could be one of the options for scheduling tasks
a

Alexander

10/20/2019, 10:45 AM
From memory
WorkManager
is also bounded by the 15 min cap - or am i getting confused
Screenshot from 2019-10-20 11-45-31.png
k

Kirill Zhukov

10/20/2019, 10:46 AM
Makes sense to me, otherwise apps would drain battery like crazy
a

Alexander

10/20/2019, 10:47 AM
Sure it makes sense, but how do usual Location apps work then? say a company like Fitbit who plot your entire day on a map?
k

Kirill Zhukov

10/20/2019, 10:48 AM
They probably use wearables to track data and then sync to the app? No clue :)
Did you check this one out yet?
a

Alexander

10/20/2019, 10:49 AM
Yes, so they recommend taking your service to the foreground - which I have started to look at - However, once in the foreground Im unsure on the best way to orchestrate the repeated task (getting long/lat)
k

Kirill Zhukov

10/20/2019, 10:52 AM
I have 0 experience with location APIs though I'm not surprised about these restrictions :)
a

Alexander

10/20/2019, 10:54 AM
I have also looked at
PeriodicWorkRequestBuilder
- but again this can only perform tasks every 15 mins
k

Kirill Zhukov

10/20/2019, 10:55 AM
Even for regular background tasks you can't completely trust OS with execution and timing of these tasks, they can get deferred or even cancelled
Say if your phone enters doze mode tasks are executed only during execution window every now and then
Saves battery big time
a

Alexander

10/20/2019, 10:56 AM
So how do location based apps get around this then?
I will read into that
k

Kirill Zhukov

10/20/2019, 11:00 AM
My coworker actually did a good talk on this stuff a while back

https://www.youtube.com/watch?v=B1V0tYdKxjY

a

Alexander

10/20/2019, 11:01 AM
OKay cool - will watch that
I think Im going to google around and look at say a foreground service that toasts the time every 10 seocnds - then work back from there
k

Kirill Zhukov

10/20/2019, 11:02 AM
Apps have to follow these restrictions, even Google maps from what I can tell
If you ever shared your location in Google maps someone you'll see that you are not getting actual live location if their phone is on standby
It could be deferred by hours
From my experience
a

Alexander

10/20/2019, 11:03 AM
There has to be a way 😛
k

Kirill Zhukov

10/20/2019, 11:03 AM
I doubt stock Android would let you do that
It's bad for battery and not good privacy wise, in your specific case
v

Vineeth P C

10/20/2019, 11:04 AM
Can't we use push notification to make the app report the location?
a

Alexander

10/20/2019, 11:04 AM
I mean i could look at streams in Kafka maybe
Or maybe a rest endpoint / webhook design
k

Kirill Zhukov

10/20/2019, 11:05 AM
Not sure how that will help you get around the restriction for background task
Not very experienced in that area either though
a

Alexander

10/20/2019, 11:05 AM
No if we abandoned the background task
k

Kirill Zhukov

10/20/2019, 11:14 AM
I'm seeing fused location API being recommend over foreground service for location tracking
👍 1
a

Alexander

10/20/2019, 11:42 AM
Yes I have an implementation of FusedLocationAPIClient
Ahh i didnt realise it can give bundled location updates in intervals - this could be good