Services Flashcards

1
Q

What is a service?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a service and the loader?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 3 types of service called?

A

Started Service; Bound Service; Intent Service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain what a Started Service is… When does it terminate? What does it return?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain what a Bound Service is…

A

A Bound Service is a service that enables other applications to bind to it to receive the service it provides.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What type of relationship dynamic do the applications involved in a Bound Service have?

A

The Bound Service and Bound Apps have a Client-Server dynamic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When does a Bound Service terminate?

A

The Bound Service will keep running so long as there is at least 1 application that is bound to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of a Bound Service…

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain what an Intent Service is…

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give an example of an Intent Service…

A

An example of an Intent Service would be downloading a video. The service would stop automatically once the download is complete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where is an Intent Service executed?

A

An Intent Service can be handled on the UI thread, however, for computationally expensive services, execution is offloaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we declare a Service in the Manifest? How do we ensure that other applications can’t start a service?

A

< services android:name=“service_name” android:exported=“false” />

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the steps to implementing a Started Service? Define each…

A
  • 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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For a Bound Service, which interface do we have to provide an implementation of in order for clients to bind to the service?

A

We must provide an implementation of IBinder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can other applications bind to a Bound Service? If so, which method to they call to do this?

A

Other applications can bind to the service through the bindService( ) method and passing an explicit intent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which type of intent must be passed to bindService()? Why is this the case?

A

An explicit intent must be used to explicitly specify the service to start. Else there may be ambiguity and the implicit intent may start an undesired service.

17
Q

Which 2 callback methods may start a Service?

A

onBind(); startService();

18
Q

Which 2 callback methods may end a service?

A

onDestroy(); stopService();