Service based Android interview questions and answers
1) Describe services and Types
A Service
is an application component that can perform long-running operations in the background, and it doesn't provide a user interface. It can run in the background, even when the user is not interacting with your application. These are the three different types of services:
- Foreground Service: A foreground service performs some operation that is noticeable to the user. For example, we can use a foreground service to play an audio track. A Notification must be displayed to the user.
- Background Service: A background service performs an operation that isn’t directly noticed by the user. In Android API level 26 and above, there are restrictions to using background services and it is recommended to use WorkManager in these cases.
- Bound Service: 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. A bound service runs only as long as another application component is bound to it.
2) Difference between Service & Intent Service
- Service is the base class for Android services that can be extended to create any service. A class that directly extends Service runs on the main thread so it will block the UI (if there is one) and should therefore either be used only for short tasks or should make use of other threads for longer tasks.
- IntentService is a subclass of Service that handles asynchronous requests (expressed as “Intents”) on demand. Clients send requests through startService(Intent) calls. The service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of workc
3) How would you update the UI of an activity from a background service?
We need to register a LocalBroadcastReceiver in the activity. And send a broadcast with the data using intents from the background service. As long as the activity is in the foreground, the UI will be updated from the background. Ensure to unregister the broadcast receiver in the onStop() method of the activity to avoid memory leaks. We can also register a Handler and pass data using Handlers. You can find more details on how to implement here.
4) Enumerate the steps in creating a bounded service through AIDL.
1. create the .aidl file, which defines the programming interface
2. implement the interface, which involves extending the inner abstract Stub class as well as implanting its methods.
3. expose the interface, which involves implementing the service to the clients.
We need to register a LocalBroadcastReceiver in the activity. And send a broadcast with the data using intents from the background service. As long as the activity is in the foreground, the UI will be updated from the background. Ensure to unregister the broadcast receiver in the onStop() method of the activity to avoid memory leaks. We can also register a Handler and pass data using Handlers. You can find more details on how to implement here.
4) Enumerate the steps in creating a bounded service through AIDL.
1. create the .aidl file, which defines the programming interface
2. implement the interface, which involves extending the inner abstract Stub class as well as implanting its methods.
3. expose the interface, which involves implementing the service to the clients.
5) What are the different ways to define the service’s IBinder interface and how the client receive it and make a connection?
Answer:
The different ways to define service’s IBinder interface and pass it to the client (just like activities) are:
- Extending the Binder class
- Using a Messenger
- Android Interface Definition Language (AIDL)
For extending the Binder class – here if the services are private to the applications and run in the same process as the client then interface be created by extending the Binder class and returning an instance of it from onBind(). The client receives the Binder and can use it to directly access public method available in either the Binder implementation or the Service.
6) What is the difference between Bound Service and Intent Service
There are many differences between IntentService and Bound Service:
- Intent Service is performing the task in background and after finishing the task, the instance of IntentService will be killed itself.
- Bound Service is running in Main Thread, while IntentService creates a worker thread and uses that thread to run the service.
- IntentService is using Handler to handle Message. While Service class needs a manual stop using stopSelf().
- IntentService implements onBind() that returns null. This means that the IntentService cannot be bound by default.
- IntentService implements onStartCommand() that sends
- Intent to queue and to onHandleIntent().
Answer:
The different ways to define service’s IBinder interface and pass it to the client (just like activities) are:
- Extending the Binder class
- Using a Messenger
- Android Interface Definition Language (AIDL)
For extending the Binder class – here if the services are private to the applications and run in the same process as the client then interface be created by extending the Binder class and returning an instance of it from onBind(). The client receives the Binder and can use it to directly access public method available in either the Binder implementation or the Service.
6) What is the difference between Bound Service and Intent Service
There are many differences between IntentService and Bound Service:
- Intent Service is performing the task in background and after finishing the task, the instance of IntentService will be killed itself.
- Bound Service is running in Main Thread, while IntentService creates a worker thread and uses that thread to run the service.
- IntentService is using Handler to handle Message. While Service class needs a manual stop using stopSelf().
- IntentService implements onBind() that returns null. This means that the IntentService cannot be bound by default.
- IntentService implements onStartCommand() that sends
- Intent to queue and to onHandleIntent().
7) If a service is implementing the method of onStartCommand(), then what are the ways a service must be stopped after the task is completed?
Using 'stopService()' that a given application service will be stopped. If the service is not running, nothing happens. Otherwise, it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started.
Using 'stopSelf()' is same as 'stopService()' but used within the service to indicate system to stop itself
Using 'stopService()' that a given application service will be stopped. If the service is not running, nothing happens. Otherwise, it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started.
Using 'stopSelf()' is same as 'stopService()' but used within the service to indicate system to stop itself
8)
I have created one background service playing music in the background. If I need to make Foreground what steps do I have to follow?
In Android, service is a component which is like activity and running in the background. Once it is started, it will run in the background even if the application goes background. But from Android O, they introduce a new concept that a user should know what task is running in the background and it also should show the notification to user with some action buttons so that the user can interact with the ongoing operation if they want. So for this, we have the best approach called as an Android Foreground Service. This foreground service is giving notification to the user that task is running in the background, for example, playing music or downloading files from the internet. And if the user wants to stop that service, then he can stop using Notification.
So to create foreground service you have to use NotificationManager.startServiceInForeground() method to send a notification to the user that the Background Service is running and if he wants he can stop it
On android services, onStart() and onBind() are the same?
A) False
In Android, does Service create new thread in background?
A) False
To develop your own service in Android, what you need to do?
A) implements onStartCommand, onBind, onUnbind, onRebind, onCreate, onDestroy methods
How to Stop a Service?
A) To stop a service from an activity we can call stopService(Intent intent) method. To Stop a service from itself, we can call stopSelf() method.
In Android, service is a component which is like activity and running in the background. Once it is started, it will run in the background even if the application goes background. But from Android O, they introduce a new concept that a user should know what task is running in the background and it also should show the notification to user with some action buttons so that the user can interact with the ongoing operation if they want. So for this, we have the best approach called as an Android Foreground Service. This foreground service is giving notification to the user that task is running in the background, for example, playing music or downloading files from the internet. And if the user wants to stop that service, then he can stop using Notification.
So to create foreground service you have to use NotificationManager.startServiceInForeground() method to send a notification to the user that the Background Service is running and if he wants he can stop it
On android services, onStart() and onBind() are the same?
A) False
In Android, does Service create new thread in background?
A) False
To develop your own service in Android, what you need to do?
A) implements onStartCommand, onBind, onUnbind, onRebind, onCreate, onDestroy methods
How to Stop a Service?
A) To stop a service from an activity we can call stopService(Intent intent) method. To Stop a service from itself, we can call stopSelf() method.
Explain Service vs intent service ?
When to use?- The Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must use threads within Service.
- The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).
How to trigger?- The Service is triggered calling to method onStartService().
- The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.
Triggered From- The Service may be triggered from any thread.
- The IntentService must be triggered from Main Thread.
Runs On- The Service runs in background but it runs on the Main Thread of the application.
- The IntentService runs on a separate worker thread.
Limitations / Drawbacks- The Service may block the Main Thread of the application.
- The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
Difference between Service , Thread , IntentService, AsyncTask ?SERVICE THREAD INTENTSERVICE ASYNCTASK When to use ? Task with no UI, but shouldn’t be too long. Use threads within service for long tasks. – Long task in general.– For tasks in parallel use Multiple threads (traditional mechanisms)
– Long task usually with no communication to main thread.
(Update)– If communication is required, can use main thread handler or broadcast intents- When callbacks are needed (Intent triggered tasks). – Long task having to communicate with main thread.– For tasks in parallel use multiple instances OR Executor
Trigger Call to method
onStartService() Thread start() method Intent Call to method execute() Triggered From (thread) Any thread Any Thread Main Thread (Intent is received on main thread and then worker thread is spawed) Main Thread Runs On (thread) Main Thread Its own thread Separate worker thread Worker thread. However, Main thread methods may be invoked in between to publish progress. Limitations /
Drawbacks May block main thread – Manual thread management– Code may become difficult to read
– Cannot run tasks in parallel.– Multiple intents are queued on the same worker thread.
– one instance can only be executed once (hence cannot run in a loop)– Must be created and executed from the Main thread
- The Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must use threads within Service.
- The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).
- The Service is triggered calling to method onStartService().
- The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.
- The Service may be triggered from any thread.
- The IntentService must be triggered from Main Thread.
- The Service runs in background but it runs on the Main Thread of the application.
- The IntentService runs on a separate worker thread.
- The Service may block the Main Thread of the application.
- The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
SERVICE | THREAD | INTENTSERVICE | ASYNCTASK | |
---|---|---|---|---|
When to use ? | Task with no UI, but shouldn’t be too long. Use threads within service for long tasks. | – Long task in general. – For tasks in parallel use Multiple threads (traditional mechanisms) | – Long task usually with no communication to main thread. (Update)– If communication is required, can use main thread handler or broadcast intents- When callbacks are needed (Intent triggered tasks). | – Long task having to communicate with main thread. – For tasks in parallel use multiple instances OR Executor |
Trigger | Call to method onStartService() | Thread start() method | Intent | Call to method execute() |
Triggered From (thread) | Any thread | Any Thread | Main Thread (Intent is received on main thread and then worker thread is spawed) | Main Thread |
Runs On (thread) | Main Thread | Its own thread | Separate worker thread | Worker thread. However, Main thread methods may be invoked in between to publish progress. |
Limitations / Drawbacks | May block main thread | – Manual thread management – Code may become difficult to read | – Cannot run tasks in parallel. – Multiple intents are queued on the same worker thread. | – one instance can only be executed once (hence cannot run in a loop) – Must be created and executed from the Main thread |
How many ways are there to create a bound service?
Three,
- Extending the Binder class
- Using a Messenger
- Using AIDL
- Extending the Binder class
- Using a Messenger
- Using AIDL
What are the steps to create a Bound service using AIDL?
Three,
- Create the .aidl file : defines the programming interface with method signatures
- Implement the interface : extend the Stub class and implement the methods
- Expose the interface to clients : Implement a Service and override onBind() to return your implementation of the Stub class
Aaaaaa
- Create the .aidl file : defines the programming interface with method signatures
- Implement the interface : extend the Stub class and implement the methods
- Expose the interface to clients : Implement a Service and override onBind() to return your implementation of the Stub class
Comments
Post a Comment