Office Hours — Today, July 8

Yesterday, July 7

Jul 8
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
Steve S.
has entered the room
Steve S.
Hi, Mark!
Mark M.
hello, Steve!
how can I help you on this fine Saturday?
Steve S.
I have a couple of quick questions today.
I have a service with some inner classes. The inner classes should outlive the service only very briefly, if at all. Should the inner classes be static?
Mark M.
ideally, yes
Steve S.
ok
Mark M.
if you're real sure about that lifetime, you're probably OK with them being non-static
but, if it's easy to make 'em static, make 'em static
Steve S.
Ok. So if they were coded correctly they could be non-static, but to be safe it would be better to make them static?
The next question is about LeakCanary. Can I use LeakCanary to look for memory leaks in a service?
Mark M.
well, the class can be coded correctly and still be leaking memory, if you are incorrect about the lifespan of those objects
Steve S.
ok
Mark M.
and, while you might be correct today about the lifespan, code changes tomorrow might change that
7:30 PM
Mark M.
so, all else being equal, static is the better answer
Steve S.
ok. I will see about making that change
Mark M.
in terms of LeakCanary, it will not automatically track leaks of services, though you can set that up AFAIK
Steve S.
do you know what I would need to do to set that up, or can you direct me to a place to look for that information?
Mark M.
I was just checking the LeakCanary docs, and they seem to have reorganized them
it should be tied into the RefWatcher, teaching it to watch your service reference
Steve S.
ok. i've used that before. i will try that
Mark M.
Steve S.
great - thank you
no more questions today
very helpful as always!
Mark M.
that was easy
Steve S.
thank you so much, Mark!
Mark M.
you're welcome!
Steve S.
have a great rest of the day!
Mark M.
you too!
7:50 PM
Steve S.
has left the room
8:10 PM
Tej S.
has entered the room
Mark M.
hello, Tej!
Tej S.
Hi Mark.
Mark M.
how can I help you today?
Tej S.
I was wondering whether you could provide some insight into starting an application from the background in Android O
Mark M.
by "starting an application", do you mean starting an activity?
Tej S.
Specifically, what modes are there ? Background, idle. etc. how to simulate testing for those modes ( possibly throught ADB commands) -- similar to forcing doze and app standby on previous versions of Android.
Mark M.
um, the modes are foreground and background
8:15 PM
Tej S.
So, as an example : I'm suppose I was to create a pending intent using getService, getActivity, getBroadcast. and then use the alarm manager. For how long would it work ?
Mark M.
for how long would what work?
Tej S.
Would getService ever throw an exception say 1 hour after the device is left untouched and the app went into background mode ?
Mark M.
I don't think so
and time has no impact here
your app is in the foreground, or it is not
Tej S.
Ok. So if the app goes background, how long is my service allowed to run before it is stopped ?
Mark M.
there is a small window (~1 minute) where your app still behaves "foreground-ish" after it moves to the background
Tej S.
Is there a way to measure this ?
Mark M.
a started, non-foreground service will be stopped after a minute, based on my testing
the documentation says "several minutes"
now, AlarmManager is not cited as one of the things that gives you a fresh window of time for background work, though AFAIK it is
Tej S.
I see, and once I'm in background mode, and an alarm fires with a pending Intent that starts a service, should I have any problems ( apart from the fact the service will be stopped in a minute unless promoted to foreground) ?
Mark M.
none tied to )
er, none tied to O
8:20 PM
Mark M.
Doze mode still applies, and I am not aware of any changes in that behavior on O
Tej S.
Thanks. Yes. So testing behavior by forcing doze and app standby through ADB should suffice ?
Mark M.
since I do not know the details of what you are testing, I cannot really answer that
the testing you did for 7.0/7.1 should be fine for O, so long as you are sure that your service will run for under a minute
or, consider switching to JobIntentService, once the v26 support libraries are released in final form
Tej S.
So I recently moved all my service code to jobs using the FirebaseJobDispatcher library. What exactly are the advantages of the JobIntentService over this library ?
Mark M.
JobIntentService is not really related to that library
JobIntentService is a replacement, of sorts, for IntentService
on Android 7.1 and below, it *is* an IntentService
but on Android 8.0+, it actually uses JobScheduler
it schedules the job to run more or less immediately, though the system might delay it a bit
a JobScheduler job can run for ~10 minutes before Android starts getting irritated
and, while I have not tried it, it seems like it should be easy to move an IntentService to a JobIntentService
8:25 PM
Mark M.
FirebaseJobDispatcher is a separate thing, designed to be a replacement for JobScheduler with backwards compatibility
I have not used it, as it requires Play Services, and I avoid things that require Play Services where I can
Tej S.
I see. I just looked at the docs for the JobIntentService. Thanks, this is useful.
*extremely useful.
Mark M.
bear in mind that JobIntentService is still part of the v26 support library beta
Tej S.
So as pertains "waking up the app" I'm looking for things like say, the awareness api -- that can bring my app to the foreground when certain conditions are met ( eg. headphones plugged in) . So what I'd really like to understand is what all can cause the app to come to the foreground, do geofences trigger this ? Do I continue to receive activity updates if my app is in background ( if so, for how long) etc.
Mark M.
that's all outside the scope of my expertise, sorry
8:30 PM
Mark M.
I have not used Play Services' geofences, nor Play Services' awareness API
Tej S.
No worries. Is there any light you can shed on the activity updates and how they're affected by backgrounding the app ?
Mark M.
I do not know what you mean by "activity updates" here
Tej S.
Things like IN_VEHICLE,
Mark M.
I have not used that API either
if it is from Play Services, odds are very good that I have never touched it
Tej S.
Ah yes, it's activity recognition.
Mark M.
and, that's a wrap for today's chat
Tej S.
Sorry for incoherence, this is the first time I'm doing a QA chat.
Thanks a ton for the help!
Mark M.
no worries
the transcript will be posted to https://commonsware.com/office-hours/ shortly
the next chat is Tuesday, 4pm US Eastern
Tej S.
Thanks!
Tej S.
has left the room
Mark M.
have a pleasant day!
Mark M.
turned off guest access

Yesterday, July 7

 

Office Hours

People in this transcript

  • Mark Murphy
  • Steve S
  • Tej Singh