Office Hours — Today, February 20

Thursday, February 18

Feb 20
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
4:05 PM
ansh
has entered the room
Mark M.
hello, ansh!
how can I help you today?
ansh
hi mark, happy new year ^_^! seeing you after a long time
Mark M.
Happy New Year to yourself as well!
ansh
i have a lot of questions regarding services
View paste

1. how do flags that we return in in on start command work? like by definition they define the behaviour on service getting killed, like weather to restart from scratch or from same intent or not start at all, but is that applicable for services that the activity killed/ those which got killed themselves?

2 who consumes the returned flag value? context.startService()  returns ComponentName i haven’t looked into that but can the caller activity/thread know what would be the status returned by  service?

3. main question : can we do something on when user swipes the app’s task from recents?

4. side question how bits work? specifically :
START_STICKY_COMPATIBILITY,
START_STICKY,
START_NOT_STICKY,
START_REDELIVER_INTENT,
3 of them are integers and 1 is 0xf?? how would the system interpret if i pass them via | command?

5 various attributes on service, can it run alongside the app if its not doing anything , close on user swipe , but run a small code before getting executed?
4:10 PM
ansh
will give more details on #5 when we come to that
Mark M.
1. "is that applicable for services that the activity killed/ those which got killed themselves?" -- I do not know what "that the activity killed" means. These values control the behavior if Android terminates your process, such as due to low memory conditions.
ansh
here i was talking about the function context.stopservice
Mark M.
those flags are ignored for stopService()
the expectation there is that since you are the one asking to stop the service, the service will be stopped
ansh
oh ok, thanks
Mark M.
the same holds true for stopSelf() from within the service
2. "who consumes the returned flag value?" -- the operating system? I am uncertain what "consumes" means here. "can the caller activity/thread know what would be the status returned by service?" -- there is nothing in the framework for that.
ansh
hmm, sounds fair. for 1 i was thinking of a case where my code would stop using the context .stop function and then again start service at some other point, so was wondering if the flags would effect it for that call
4:15 PM
Mark M.
your second startService() will trigger a fresh call to onStart(), where your service will return a new value
3. "can we do something on when user swipes the app’s task from recents?" -- if you have a service running, in theory you should be informed of the task removal via onTaskRemoved(): https://developer.android.com/reference/android...
(sorry, that link will not work due to broken URL parsing by this chat service)
however, I would not rely upon onTaskRemoved(), as some device manufacturers have really messed with the overview/recent tasks screen
if you get called with onTaskRemoved() and want to do something, great, but I would assume that it is possible that your process will be terminated without onTaskRemoved() being called
ansh
hmm, so any other option apart from that?
Mark M.
no
at least, none that I know of
4. "how would the system interpret if i pass them via | command?" -- the phone might catch on fire :-)
ansh
come on XD
Mark M.
those are mutually-exclusive values, and I have no idea what the OS will do if you try combining them that way
and I do not understand your question #5 at all, sorry!
ansh
but there are multiple times that i have used the | operator. like passing flags for intent
Mark M.
yes -- this is not one of those cases
this is not a bitfield
ansh
oh okay
4:20 PM
ansh
also i wanted to know one more thing, services can run in a seperate process , right? so when app is cleared from recents, only the process holding the activity will be removed , right? not the one with service
Mark M.
"services can run in a seperate process , right?" -- correct
'so when app is cleared from recents, only the process holding the activity will be removed , right? not the one with service" -- in theory, I believe that this is correct
however, as with onTaskRemoved(), I would not rely upon that behavior
ansh
so would the service still get a callback ontaskremoved? assuming the hypothetical device manufacturer foogle which gives callback
Mark M.
"so would the service still get a callback ontaskremoved?" -- AFAIK, yes, but I have not tried this
ansh
hmm okay
1 more thing, from what i know services by default run on main thread. so if i have a service running that is doing nothing, will it effect the app experience that is also running on the main thread?
Mark M.
"from what i know services by default run on main thread" -- a service is an object. Objects in Java/Kotlin do not run on threads. Methods and functions run on threads. The callback methods on a service are called on the main application thread.
"so if i have a service running that is doing nothing, will it effect the app experience that is also running on the main thread?" -- if you truly mean "doing nothing", then no. And, you control the threads used for your own work.
4:25 PM
ansh
ohh , that explains a lot... for so long i have thought of services like some kind of magic. but if they are just objects , then... damn that's like having a simple interface instance in an activity that could be used to call some functions defined somewhere else
Mark M.
like activities, services have particular roles in the framework and OS
however, otherwise, they are ordinary objects
ansh
hmm, nice, i got a mind blowing revelation at 3 am in night, courtesy to you XD
please add these things in the books too
Mark M.
well, services are not really being used as much anymore
that is why I do not have a chapter on them in *Elements of Android Jetpack*
WorkManager is handling a lot of the use cases for services now
ansh
yeah that brings another question. and yes i had to go through the busy coder book before this meeting
so yes, if i create a service class extending service it will be a background service if i started it via context.startService(), right?
Mark M.
yes, with all the limitations inherent in a background service, most notably a maximum runtime of one minute on Android 8.0 and higher
4:30 PM
ansh
even when app is running and visible to user?
Mark M.
if the app is running and visible to the user, you do not need a service, usually
and, to be honest, I do not remember what the behavior is with startService() for a process that is already in the foreground
just about everything that I do has moved either to foreground services, to WorkManager, or to specialized services that the OS binds to (e.g., a TileService)
ansh
:-o
also since background services have resticitions, they will be again killed off right when app gets cleared from recents right?
do we know what will happen first, stop service or ontaskRemoved()?
Mark M.
frequently, they do not live long enough to matter -- one minute is a fairly short period of time
I assume that onTaskRemoved() is called before onDestroy(), if that is what you mean
but, I have not needed to use onTaskRemoved(), so I may be guessing incorrectly
ansh
I just need to show a toast like thing specifically for this closed-from-recents behavior
Mark M.
I am uncertain how you are going to show UI from the background on modern versions of Android
4:35 PM
Mark M.
you might be able to use an actual Toast, so long as you are just showing a bit of text
but other things, like starting an activity from the background, are no longer options
basically, you are running into a bunch of things that developers have abused over the years, so Google is adding more and more restrictions, with an eye towards power consumption, security, and so on
ansh
hmm i see. just got a task from work, that's why had to go through the various docs of these services, otherwise i too use work manager and intent service for most of my tasks
hehe, you caught a little yard thief
anyways thanks for the insights. always learn good info from you
Mark M.
happy to help!
ansh
View paste
By the way, i just wants to say a big thank you to you and your books. i started to work in this industry 5 months ago and the whole credit goes to your books and various other people uploading their knowledge on the internet.
Never would have thought a free time hobby could make me some daily bread , many thanks!
Mark M.
I am glad that this is working out for you!
ansh
what are your thoughts on jetpack compose?
4:40 PM
Mark M.
it is going to be huge, but I feel like they are rushing the beta release
4:40 PM
Mark M.
if you are interested, I have a free weekly newsletter on Compose: https://jetc.dev/
ansh
can we expect the busy coder guide's to this evolution of android development
Mark M.
I am working on *Elements of Jetpack Compose*
that will be a guide to Compose and Compose UI for people with some Android experience
ansh
yes that will be awesome!
Mark M.
eventually, assuming that Compose becomes popular, I will revise *Elements of Android Jetpack* and *Exploring Android* to use Compose
ansh
i haven't looked into compose yet, but isn't it like mixing of xml and kotlin into single file? does that thing even work with the usual seperate kotlin-xml app structure?
Mark M.
you will not use XML layout resources anymore, in a Compose-based app
ansh
(from what i heard ^^ )
Mark M.
with Compose, you define the UI in Kotlin
4:45 PM
ansh
hmmm... that will be interesting. however i have heard that using kotlin alone makes a significant impact on build times and app sizes. won't compose effect that even more?
and i just remembered a small query regarding manifest ^_^!
do we still have time?
Mark M.
sure -- the chat is on for another 15 minutes
I have not tried doing a comparison of build times between a pure-Java project and a pure-Kotlin project with the same content
I do not find Jetpack Compose to make build times dramatically worse, in part due to some better compiler options that JetBrains has added to Kotlin
ansh
so i was wondering how manifest really works? like the attributes we define there, like say for service, if i define an exported = true, would the manifest be like, guiding the system to start that service in exported mode?
Mark M.
well, basically, your manifest entries get put into a system database, along with those of other apps on the device
whether the component is exported or not is part of the database
and when something tries interacting with your service, the OS will check that database and confirm that the service is exported
4:50 PM
ansh
ohhh, so os will be checking such db on every activity start /app start/service start , etc , right?
Mark M.
effectively, yes
I assume a bunch of this is cached in system RAM by a core OS process
ansh
and why do each of those components have some attributes by default? like say all the views have width/height attribute or all the components have "icon" attribute
Mark M.
I cannot tell you why the early Android developers made the decisions that they made back then
bear in mind that some bits of Android are 15 years old at this point
ansh
no i meant like even if am creating a custom view, it is also getting those attributes
is it just because i extended the view class?
Mark M.
mostly, yes
ansh
hmm okay, i have always felt the xml-java or xml-kotlin interaction as some magic
4:55 PM
ansh
but now some things feel more clear, thanks
Mark M.
it involves a lot of complex code, but in the end, it is all just objects
ironically, Jetpack Compose will involve things much closer to "magic"
ansh
haha, then i will be first looking into your book before looking into the docs ;)
thanks for clearing my doubts, mark. i will be leaving now. good day!
Mark M.
take care!
ansh
has left the room
Mark M.
turned off guest access

Thursday, February 18

 

Office Hours

People in this transcript

  • ansh
  • Mark Murphy