Services Flashcards

1
Q

Foreground Service

A

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn’t interacting with the app.

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

Background Service

A

A background service performs an operation that isn’t directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

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

Bound Service

A

A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

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

Service Class

A

This is the base class for all services. When you extend this class, it’s important to create a new thread in which the service can complete all of its work; the service uses your application’s main thread by default, which can slow the performance of any activity that your application is running.

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

IntentService Class

A

This is a subclass of Service that uses a worker thread to handle all of the start requests, one at a time. This is the best option if you don’t require that your service handle multiple requests simultaneously. Implement onHandleIntent(), which receives the intent for each start request so that you can complete the background work.

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

Service vs Thread

A

A service is simply a component that can run in the background, even when the user is not interacting with your application, so you should create a service only if that is what you need.

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

Service.onStartCommand

A

The system invokes this method by calling startService() when another component (such as an activity) requests that the service be started. When this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is complete by calling stopSelf() or stopService(). If you only want to provide binding, you don’t need to implement this method.

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

Service.onBind

A

The system invokes this method by calling bindService() when another component wants to bind with the service (such as to perform RPC). In your implementation of this method, you must provide an interface that clients use to communicate with the service by returning an IBinder. You must always implement this method; however, if you don’t want to allow binding, you should return null.

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

Service.onCreate

A

The system invokes this method to perform one-time setup procedures when the service is initially created (before it calls either onStartCommand() or onBind()). If the service is already running, this method is not called.

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

Service.onDestroy

A

The system invokes this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, or receivers. This is the last call that the service receives.

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

onStartCommand -> START_NOT_STICKY

A

If the system kills the service after onStartCommand() returns, do not recreate the service unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.

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

onStartCommand -> START_STICKY

A

If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent unless there are pending intents to start the service. In that case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands but are running indefinitely and waiting for a job.

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

onStartCommand -> START_REDELIVER_INTENT

A

If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file.

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