Android Services
Hello friends hi. This post is regarding all about Service in Android. Service is one of the most important component of android application.
In this post you will learn about the following points which is also the most important for any android interview...
1)- What is Service?
2)- What are the types of Service?
3)- What is the difference between startService() and bindService()?
4)- What is IntentService and how it is differ from Service?
5)- Passing data between Activity and Service?
6)- What is Looper, Message Queue and Handler?
So let's get start....
1)- What is Service?
The definition is very simple:- A service is an android component that perform long running operation in
background without providing the user interface. A service can run in background even if the user
switches to the another application.
2)- What are types of service?
There are mainly three types of the service....
a)- Foreground Service
b)- Background Service
c)- Bound Service
Let's discuss it one by one....
a)- Foreground Service: A Foreground Service perform some background operations that are
noticeable to the User. For example: A music player application that play an audio.
A Foreground Service must display a notification and it can run continuously even if the user is not
interacting with the application.
b)- Background Service: A Background Service perform some background operations that are not
directly noticed by the user. For example: If an application use a service to compact its storage.
Note: As of Android 8.0, Background Service has some limitations. In API level 26 or higher,
Schedule task using Work Manager.
c)- Bound Service: A Service is bound when an application component bind to it by calling onBind()
method. A bound service offers a client-server interface that allow component to interact with
the service, send request and receive result. A bound service runs only as long as the another app
component is bind to it. Multiple component can bind to the service at once but when all of them
unbind, the service is destroyed.
On the other hand, Bound Service allow components (Activity, Service) to bind to it and interact with it
via Inter-Process-Communication (IPC).
3)- What is the difference between startService() and bindService() method?
*** startService():- It starts a service to perform a one time operation and does not return any result.
-----Where as-----
*** bindService():- It bind to a service allowing interaction with the service through an interface and
return a result.
4)- What is IntentService and how it is differ from Service?
IntentService is a subclass of Service that handles asynchronous (expressed as Intent) request on
demand. It automatically start a worker thread to handle each Intent and stop itself when all
the requests are handled. Here is the code snippet for IntentService......
5)- Passing data between Activity and Service?
We can pass the data between an Activity and a Service using Intent or a Bound Service with a Binder.....
*** Using Intent:- Here is the code snippet for passing data between an Activity and a Service....
*** bindService:- Here is the code snippet for passing data between an Activity and a bound service....
6)- What is Looper, Message Queue and Handler?
a)- Looper:- It is a class helps in keeping a thread alive by its prepare method. It keep looking for messages in Message Queue for execution.
b)- Message Queue:- This Queue consists of messages or runnable that would like to be executed. Messages are not directly added to the queue but we have to use the Handler class
c)- Handler:- This class helps to do two things-
** (i)-- Scheduling the messages and runnable to be excited in the future.
** (ii)-- To perform actions on different threads than your own.



Comments
Post a Comment