Services Flashcards
What is a service?
A service is one of the 4 android components. It enables processes to continue running in the background, even if the application is closed. For example, a reminders app or alarm clock app.
What is the difference between a service and the loader?
A service runs off of the UI thread in the background, and is completely independent from the Activity lifecycle. The Loader is tied to the UI thread and activity lifecycle and changes the UI.
What are the 3 types of service called?
Started Service; Bound Service; Intent Service.
Explain what a Started Service is… When does it terminate? What does it return?
A Started Service is one which is called via startService( ), and runs in the background indefinitely until another component stops it. It doesn’t return anything.
Explain what a Bound Service is…
A Bound Service is a service that enables other applications to bind to it to receive the service it provides.
What type of relationship dynamic do the applications involved in a Bound Service have?
The Bound Service and Bound Apps have a Client-Server dynamic.
When does a Bound Service terminate?
The Bound Service will keep running so long as there is at least 1 application that is bound to it.
Give an example of a Bound Service…
An example of a Bound Service would be an Activity on a Music Player App that is dynamically displaying the current data of the song playing.
Explain what an Intent Service is…
An Intent Service is type of service that is used to handle long running tasks of the main thread. They are designed to perform a single operation. When this is completed, the service stops automatically.
Give an example of an Intent Service…
An example of an Intent Service would be downloading a video. The service would stop automatically once the download is complete.
Where is an Intent Service executed?
An Intent Service can be handled on the UI thread, however, for computationally expensive services, execution is offloaded.
How do we declare a Service in the Manifest? How do we ensure that other applications can’t start a service?
< services android:name=“service_name” android:exported=“false” />
What are the steps to implementing a Started Service? Define each…
- startService() -> Pass in explicit intent with any associated data.
- onCreate() -> Creates in memory. Call with other lifecycle callbacks.
- onStartCommand() -> Starts the service.
- stopSelf() or stopService()
For a Bound Service, which interface do we have to provide an implementation of in order for clients to bind to the service?
We must provide an implementation of IBinder.
Can other applications bind to a Bound Service? If so, which method to they call to do this?
Other applications can bind to the service through the bindService( ) method and passing an explicit intent.