Android user interface is restricted to perform long running jobs to make user experience smoother.
A typical long running tasks can be periodic downloading of data from internet, saving multiple records into database, perform file I/O, fetching your phone contacts list, etc. For such long running tasks, Service is the alternative.
service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface).
A typical long running tasks can be periodic downloading of data from internet, saving multiple records into database, perform file I/O, fetching your phone contacts list, etc. For such long running tasks, Service is the alternative.
- A service is a application component used to perform long running tasks in background.
- A service doesn’t have any user interface and neither can directly communicate to an activity.
- A service can run in the background indefinitely, even if component that started the service is destroyed.
- Usually a service always performs a single operation and stops itself once intended task is complete.
- A service runs in the main thread of the application instance. It doesn’t create its own thread. If your service is going to do any long running blocking operation, it might cause Application Not Responding (ANR). And hence, you should create a new thread within the service.
service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface).
The service runs in the background indefinitely even if application is destroyed.
Moreover, service can be bounded by a component to perform interactivity and inter process communication (IPC).
The android.app.Service is subclass of ContextWrapper class.
Different Type of Service:-
Different Type of Service:-
- Unbound Service
- Bound Service
It is a kind of service which runs in the background indefinitely, even if the activity which started this service ends.
It is a kind of service which runs till the lifespan of the activity which started this service
Life Cycle of Android Service
There can be two forms of a service.The lifecycle of service can follow two different paths: started or bound.
- Started
- Bound
1) Started Service
A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.
Difference between Service and Intentservice
Service
is a base class of service implementation. Service
is run in the application's main thread which may reduce the application performance. Thus, IntentService
, which is a direct subclass of Service is available to make things easier.The
IntentService
is used to perform a certain task in the background. Once done, the instance of IntentService
terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.Differences
Service
class uses the application's main thread, whileIntentService
creates a worker thread and uses that thread to run the service.IntentService
creates a queue that passes one intent at a time toonHandleIntent()
. Thus, implementing a multi-thread should be made by extendingService
class directly.Service
class needs a manual stop usingstopSelf()
. Meanwhile,IntentService
automatically stops itself when it finishes execution.IntentService
implementsonBind()
that returnsnull
. This means that theIntentService
can not be bound by default.IntentService
implementsonStartCommand()
that sends Intent to queue and toonHandleIntent()
.
IntentService
. Firstly, to implement the constructor. And secondly, to implement onHandleIntent()
. For other callback methods, the super is needed to be called so that it can be tracked properly.
No comments:
Post a Comment