Feb 15 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Mike | has entered the room |
Mike |
Hi
|
Mark M. |
hello, Mike!
|
Mark M. |
how can I help you today?
|
Mike |
hello Mark
|
Mike |
Could you please help me clarify something
|
Mike |
I need to write an app which tracks device location in background
|
Mike |
I'm using Fused Location API
|
Mike |
I know getting device location continuously drains the battery
|
Feb 15 | 4:00 PM |
Mike |
But the requirement is, when user does specific actions in the app, the app should start monitoring the location
|
Mike |
it has to do this even on reboot if the app's custom state requires it
|
Mike |
my question is
|
Mike |
I've been reading that I need to keep a Wake Lock and a foreground service
|
Mark M. |
I am not certain if that is necessary with the fused location API, but I have not tried using that from a background app
|
Mike |
per my understand Wake Lock keeps CPU awake (although with Android 6, we have Doze mode and App Standby)
|
Mark M. |
correct
|
Mike |
and Foreground service is to keep process alive
|
Mark M. |
correct, which you may not need, if you are using the PendingIntent version of the fused location API methods
|
Mike |
right
|
Mike |
Im having trouble understanding the role of wakelake vs foreground service when in regards with keeping service alive
|
Mike |
wakelock*
|
Mark M. |
a WakeLock does not keep a process alive
|
Mark M. |
it just keeps the CPU powered on
|
Mike |
right
|
Mark M. |
a foreground service helps keep the process alive
|
Mike |
so foreground service is to tell Android not to apply its optimization mechanism related to priotization
|
Feb 15 | 4:05 PM |
Mike |
wakelock is strictly an optimization for CPU
|
Mike |
both need to be applied, correct?
|
Mark M. |
a foreground service raises the importance of its process to be on par with the foreground UI
|
Mark M. |
possibly neither need to be applied
|
Mike |
:)
|
Mark M. |
it depends on what your app does and how the fused location API behaves
|
Mike |
how come
|
Mike |
correct
|
Mike |
in my case, my pending intent used when initiating tracking location, does not need a foreground service
|
Mark M. |
it shouldn't need a foreground service, as the PendingIntent (when invoked) will trigger the creation of a fresh process for you, if needed
|
Mark M. |
the bigger question is the WakeLock
|
Mike |
right
|
Mark M. |
my hope would be that if you have a LocationRequest tied to a PendingIntent, that power management is Google's problem, not yours
|
Mark M. |
and so you would skip both the WakeLock and the foreground service, and get location updates as requested
|
Mark M. |
however, I have not tried this
|
Mark M. |
so, as the saying goes, YMMV
|
Mike |
but if the pendingintent is runing a long task, it might be subject to kill. correct?
|
Mike |
that's why a wake lock woudl be needed I think
|
Mark M. |
ah, I see
|
Mark M. |
yes, while you are doing work related to an incoming location update, you may want a WakeLock
|
Mike |
actually it's a IntentService
|
Mark M. |
and, if that work will take a substantial period of time, you may want a foreground service
|
Mike |
so the pending intent actually will start an intentservice
|
Mark M. |
however, ideally, you only need those things while processing an individual location update, not 24x7
|
Mike |
right
|
Mike |
correct
|
Mark M. |
ummm... I would have the PendingIntent start a WakefulBroadcastReceiver, which starts the IntentService
|
Mike |
right
|
Mark M. |
or, use my WakefulIntentService and a regular BroadcastReceiver
|
Feb 15 | 4:10 PM |
Mike |
and intentservice need to keep a wakelock and set itself as foreground?
|
Mark M. |
if you use WakefulBroadcastReceiver, it handles the WakeLock
|
Mark M. |
similarly, if you use my WakefulIntentService, it handles the WakeLock
|
Mark M. |
the idea is that those components wrap up the WakeLock management, so you do not need to worry about it
|
Mark M. |
whether you need a foreground service would depend on how long the work might take
|
Mike |
I don't know how much it takes
|
Mike |
it needs to send GPS locations to server
|
Mark M. |
if the work should be done in a matter of seconds, a foreground service should not be necessary
|
Mark M. |
if the work might take minutes or hours, a foreground service is a good idea
|
Mike |
the app writes location in database and also tries to send all pending in database to backend
|
Mike |
another thing:
|
Mike |
when connecting to google play services
|
Mark M. |
IMHO, the work to be done when receiving the location update is only to write the location to the database (or Tape queue or something)
|
Mark M. |
then, use JobScheduler, AlarmManager, SyncAdapter, or something for the periodic uploads
|
Mike |
ok
|
Mike |
by the way, I find scheduling tasks so tricky ...
|
Mark M. |
it is definitely a pain
|
Mike |
with all the changes with doze mode
|
Mike |
my app has to run on ANdroid 4.4+
|
Mark M. |
I am looking to (finally) cover SyncAdapter in an upcoming book update
|
Mike |
nice! looking forward for it
|
Mike |
one more question
|
Mike |
the code which connects to google play services
|
Mike |
and then calls requestLocationUpdates on the location manager
|
Feb 15 | 4:15 PM |
Mike |
im not sure if it needs to be in a service which has stay alive
|
Mike |
I mean
|
Mike |
once you called requestLocationUpdates
|
Mike |
the process which called this
|
Mike |
does it have to stay running
|
Mike |
?
|
Mark M. |
if you are using the PendingIntent version of the method, it should not need to stay running
|
Mark M. |
if you are using the LocationCallback version of the method, it does need to stay running
|
Mike |
Im using the pending intent which needs to start that service to write locations to DB and also send them
|
Mike |
the reason I am asking is becasue
|
Mike |
I dont know how to stop receiving the updates which was initiated with pendinginent
|
Mike |
requestLocationUpdates returns a PendingResult
|
Mark M. |
call removeLocationUpdates() with an equivalent PendingIntent
|
Mike |
oh
|
Mike |
hmm
|
Mike |
because PendingIntent stays in OS
|
Mike |
?
|
Mike |
becauyse that's how it actually works
|
Mark M. |
I would phrase it more as "a PendingIntent represents an OS-managed token that survives process termination"
|
Mike |
right
|
Mark M. |
Google's Play Services process needs to stay running, as you cannot persist a PendingIntent
|
Mark M. |
but your process should not need to stay running
|
Feb 15 | 4:20 PM |
Mark M. |
and, when you want to stop the location updates, call removeLocationUpdates() with "The PendingIntent that was used in requestLocationUpdates(GoogleApiClient, LocationRequest, PendingIntent) or is equal as defined by equals(Object)."
|
Mark M. |
(quoting the docs)
|
Mike |
I read about really edgy situations when Google PLay Services gets updated, but I don't even want to think about that situation, it really hurts
|
Mike |
right! that phrase " or is equal as defined by equals(Object)." is key
|
Mark M. |
yes, there will be corner cases that you cannot really address
|
Mike |
oh man
|
Mike |
thank you so much
|
Mike |
I have nobody I can talk to :)
|
Mark M. |
I am happy to be useful!
|
Mike |
btw,
|
Mike |
I've never quite seen a good explanation in one phrase about the difference between wakelock and foreground service
|
Mike |
I think many people tend not to understand it
|
Mike |
they use it because they read about it
|
Mike |
but I think some don't really understand
|
Mark M. |
well, that is akin to expecting to see a phrase about the difference between a banana and a knife
|
Mark M. |
they are related, in that you might use a knife with a banana
|
Mike | |
Mike |
scary thing
|
Mark M. |
but they are not so tightly coupled that you would equate a knife with a banana
|
Feb 15 | 4:25 PM |
Mike |
you are saying "another". I'm having a hard time finding another article on your blog about doze
|
Mark M. |
yes, I had forgotten about that case
|
Mike |
doze issues
|
Mike |
is there another scenario I need to keep i mind?
|
Mark M. |
there, I meant "another" with respect to other Doze-related challenges
|
Mark M. |
such as the documented ones
|
Mike |
I see
|
Mike |
one more thing
|
Mike |
testing getting location updates is really really a pain
|
Mike |
the other day I tried everything I could to see how getting location is NOT working
|
Mike |
i wanted to see how it's not working when I dont have a wakelock AND it's not a foregrounded
|
Mike |
it kept running
|
Mike |
even with screen off
|
Mike |
it kept running
|
Mike |
I didn't understand why
|
Mark M. |
well, as I wrote, that'
|
Mike |
oh
|
Mike |
wait a minute
|
Mark M. |
that's ideally how it should work
|
Mike |
it was firing off my service
|
Mark M. |
right, based on the PendingIntent
|
Mike |
so GPS wakes up the CPU?
|
Mike |
im having trouble understanding this
|
Mike |
when does the device really in sleep mode
|
Mike |
i was testing on ANdroid 4.4 and Android 5
|
Mark M. |
Play Services is managing the power
|
Mark M. |
whether it is using a continuous WakeLock or something else is up to them, plus the specifics of your LocationRequest
|
Feb 15 | 4:30 PM |
Mark M. |
GPS itself should not wake up the CPU, AFAIK
|
Mike |
so you're saying it's not guaranteed to get GPS location update with the pendingintent?
|
Mike |
note im not talking about Doze mode
|
Mike |
at least IM trying to understadn how it works on Android 4.4 and Android 5
|
Mark M. |
in your LocationRequest, you indicate the priority for the location updates
|
Mark M. |
PRIORITY_HIGH_ACCURACY, PRIORITY_LOW_POWER, etc.
|
Mike |
right, and power scheme I want to target I think
|
Mark M. |
that helps Play Services determine what location technologies to use and what else to do (e.g., WakeLock)
|
Mike |
oh I see
|
Mike |
under the hood they are using wakelocks too I guess
|
Mike |
same tools
|
Mark M. |
exactly what Play Services does is up to Google, probably varies by Play Services release, is undocumented, etc.
|
Mark M. |
presumably it uses WakeLocks
|
Mark M. |
however, I cannot rule out that they have special hooks to make WakeLocks do more than what we have in the SDK
|
Mike |
make sense
|
Mark M. |
in theory, Play Services is "just another app"
|
Mike |
OK
|
Mike |
thank you very much again
|
Mark M. |
you're welcome!
|
Feb 15 | 4:35 PM |
Mike |
you're usually online here only in the hours I see in the Office Hours calendar, correct?
|
Mark M. |
yes
|
Mark M. |
I close the chat room after the office hours ends
|
Mike |
thank you again, have a great day \ evening
|
Mark M. |
you too!
|
Feb 15 | 4:45 PM |
Mike |
sorry Mark, I can't see all the discussion. Scrolling up shows me part of the discussion :(
|
Mike |
only after 4:15 PM
|
Mike |
is it possible to see all of it?
|
Mark M. |
probably not right now
|
Mark M. |
I will be posting the entire transcript to https://commonsware.com/office-hours/ once the chat ends
|
Mike |
ok thanks
|
Mark M. |
so, if you can wait ~15 minutes, you will have access to it all then
|
Feb 15 | 5:00 PM |
Mark M. |
that's the end of today's chat
|
Mark M. |
as I wrote, the transcript will go up on https://commonsware.com/office-hours/ shortly
|
Mike |
thank you
|
Mark M. |
the next chat is tomorrow at 7:30pm US Eastern
|
Mark M. |
have a pleasant day!
|
Mike | has left the room |
Mark M. | turned off guest access |