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!
|
Mark M. |
how can I help you on this fine Saturday?
|
Steve S. |
I have a couple of quick questions today.
|
Steve S. |
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
|
Mark M. |
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?
|
Steve S. |
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
|
Jul 8 | 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
|
Mark M. |
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
|
Steve S. |
no more questions today
|
Steve S. |
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!
|
Jul 8 | 7:50 PM |
Steve S. | has left the room |
Jul 8 | 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
|
Jul 8 | 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
|
Mark M. |
and time has no impact here
|
Mark M. |
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
|
Mark M. |
the documentation says "several minutes"
|
Mark M. |
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 )
|
Mark M. |
er, none tied to O
|
Jul 8 | 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
|
Mark M. |
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
|
Mark M. |
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
|
Mark M. |
JobIntentService is a replacement, of sorts, for IntentService
|
Mark M. |
on Android 7.1 and below, it *is* an IntentService
|
Mark M. |
but on Android 8.0+, it actually uses JobScheduler
|
Mark M. |
it schedules the job to run more or less immediately, though the system might delay it a bit
|
Mark M. |
a JobScheduler job can run for ~10 minutes before Android starts getting irritated
|
Mark M. |
and, while I have not tried it, it seems like it should be easy to move an IntentService to a JobIntentService
|
Jul 8 | 8:25 PM |
Mark M. |
FirebaseJobDispatcher is a separate thing, designed to be a replacement for JobScheduler with backwards compatibility
|
Mark M. |
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.
|
Tej S. |
*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
|
Jul 8 | 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
|
Mark M. |
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.
|
Tej S. |
Thanks a ton for the help!
|
Mark M. |
no worries
|
Mark M. |
the transcript will be posted to https://commonsware.com/office-hours/ shortly
|
Mark M. |
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 |