Feb 20 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Feb 20 | 4:05 PM |
ansh | has entered the room |
Mark M. |
hello, ansh!
|
Mark M. |
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
|
ansh |
View paste
|
Feb 20 | 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()
|
Mark M. |
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
|
Mark M. |
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
|
Feb 20 | 4:15 PM |
Mark M. |
your second startService() will trigger a fresh call to onStart(), where your service will return a new value
|
Mark M. |
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...
|
Mark M. |
(sorry, that link will not work due to broken URL parsing by this chat service)
|
Mark M. |
however, I would not rely upon onTaskRemoved(), as some device manufacturers have really messed with the overview/recent tasks screen
|
Mark M. |
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
|
Mark M. |
at least, none that I know of
|
Mark M. |
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
|
Mark M. |
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
|
Mark M. |
this is not a bitfield
|
ansh |
oh okay
|
Feb 20 | 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
|
Mark M. |
'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
|
Mark M. |
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
|
ansh |
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.
|
Mark M. |
"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.
|
Feb 20 | 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
|
Mark M. |
however, otherwise, they are ordinary objects
|
ansh |
hmm, nice, i got a mind blowing revelation at 3 am in night, courtesy to you XD
|
ansh |
please add these things in the books too
|
Mark M. |
well, services are not really being used as much anymore
|
Mark M. |
that is why I do not have a chapter on them in *Elements of Android Jetpack*
|
Mark M. |
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
|
ansh |
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
|
Feb 20 | 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
|
Mark M. |
and, to be honest, I do not remember what the behavior is with startService() for a process that is already in the foreground
|
Mark M. |
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
|
ansh |
also since background services have resticitions, they will be again killed off right when app gets cleared from recents right?
|
ansh |
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
|
Mark M. |
I assume that onTaskRemoved() is called before onDestroy(), if that is what you mean
|
Mark M. |
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
|
Feb 20 | 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
|
Mark M. |
but other things, like starting an activity from the background, are no longer options
|
Mark M. |
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
|
ansh |
hehe, you caught a little yard thief
|
ansh |
anyways thanks for the insights. always learn good info from you
|
Mark M. |
happy to help!
|
ansh |
View paste
|
Mark M. |
I am glad that this is working out for you!
|
ansh |
what are your thoughts on jetpack compose?
|
Feb 20 | 4:40 PM |
Mark M. |
it is going to be huge, but I feel like they are rushing the beta release
|
Feb 20 | 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*
|
Mark M. |
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
|
Feb 20 | 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?
|
ansh |
and i just remembered a small query regarding manifest ^_^!
|
ansh |
do we still have time?
|
Mark M. |
sure -- the chat is on for another 15 minutes
|
Mark M. |
I have not tried doing a comparison of build times between a pure-Java project and a pure-Kotlin project with the same content
|
Mark M. |
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
|
Mark M. |
whether the component is exported or not is part of the database
|
Mark M. |
and when something tries interacting with your service, the OS will check that database and confirm that the service is exported
|
Feb 20 | 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
|
Mark M. |
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
|
Mark M. |
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
|
ansh |
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
|
Feb 20 | 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
|
Mark M. |
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 ;)
|
ansh |
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 |