In my current Android Project, I am starting a Service via startService()
, then afterwards I am binding to the Service with bindService()
. (I do this because I want to have a started Service I am able to communicate with, nevermind)
After the Context is bound to the Service, usually onServiceConnected()
of my ServiceConnection
(wich I created earlier) is called if i understood this right.
Can I assume, that onServiceConnected()
is only called after all of my code in the onCreate
of my Service is executed?
To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
There are three ways of creating bound services: First is by extending the binder class. Second is by using a messenger. Third, is by using AIDL or Android Interface Definition Language.
stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed. Clients can also use Context. bindService() to obtain a persistent connection to a service.
Can I assume, that onServiceConnected() is only called after all of my code in the onCreate of my Service is executed?
Yes you can.
According to the service lifecycle diagram onBind()
is not called until after onCreate()
has completed.
The documentation states that the system calls onServiceConnected()
to deliver the IBinder
returned by the service's onBind()
method.
Therefore onCreate()
is completed before onServiceConnected()
is called since it is dependent on the return value of onBind()
which occurs after onCreate()
. You can also see this in the diagram where it says "Clients are bound to service".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With