·
A service does not provide a user interface
·
Because
an unbounded number of services can ask to be running for an unknown amount of
time.
·
Thread
must be "embedded" in the service, not the opposite. The service acts
as an entry point, in which you can create. several threads if you like.
·
Service
never runs in a dedicated /thread/. By default it runs in the main application
thread, and may optionally run in a separate /process
- In Android, even if the Service runs in the background, it runs on the Main Thread of the application. So, if at the same time if you have an activity displayed, the running service will take the main thread and the activity will seem slow. It is important to note that a Service is just a way of telling Android that something needs to run without a user interface in the background while the user may not interacting with your application. So, if you expect the user to be interacting with the application while the service is running and you have a long task to perform in a service, you need to create a worker thread in the Service to carry out the task.
- Services are a way to tell Android that certain code in your application is important to the user, and Android should make an effort to keep it running even when it doesn't have active activities.
- Your service will want to spawn its own thread or asynctask to do your heavy work or else you will get a forceclose
|
Service
|
Thread
|
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 |
A service is an application component that can run some long
running task in the background without the need for a user interface. Some
other application component can start the service and this service will then
keep on running even if the user switches to another application.
Unbounded
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely (unbounded), even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely (unbounded), even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
Bound
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, get 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.
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, get 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.
·
Service is use to long running operations
downloading data and uploading of data ,processes,updating content provider etc
·
Service have high priority then activity
·
Service can also be restart
·
Service have same priority as activity
1:- Service
background services:-
By default service run the main thrad as
main thread
Service which can run in the process of
application some time called as local service
2:- platform
service and custome service:-
It is use to designe
responcive application it can fatch data by it once application is started at
once it can presist fresh data to user
3:- defining custom services:-
You can check if the service was restarted via the Intent.getFlags() method. START_FLAG_REDELIVERY (in case the
service was started with Service.START_REDELIVER_INTENT) or START_FLAG_RETRY (in case the service
was started with Service.START_STICKY) is passed.
3.1:- stoping service:-
Stopservice() method
use to stop service
It is tipicaley finish its work in application
4:- Binding service :-
Binding service from
Activity:-
If activity wants to interect with service directley it can use bindservice()method to
start service
This method requires a ServiceConnection
object as a parameter which is called on the service start and when finishing
its onBind() method. This
method returns a IBinder
object to the ServiceConnection.
This
IBinder object can
be used by the activity to communicate with the service. When the binding process has finished, the
onStartCommand() method in the service is called with the
Intent object used for the bindService() method.
Service can run separate process :-
Running service in its own process gives its own memory
address space and garvage cullecter virtual mechine not effect to application
if seprate process is to executed .
Android provides the
LocalBroadcastManager class
in the support library v4. This is a helper class to register for and send
broadcasts of Intents to local objects within your process. This approach
improves security as the broadcast events are only visible within your process
and is faster than using standard events.|
Callback
|
Description
|
|
onStartCommand()
|
The system calls this method when
another component, such as an activity, requests that the service be started,
by calling startService(). If you implement this method, it is your
responsibility to stop the service when its work is done, by calling stopSelf()
or stopService() methods.
|
|
onBind()
|
The system calls this method when
another component wants to bind with the service by calling bindService().
If you implement this method, you must provide an interface that clients use
to communicate with the service, by returning an IBinder object. You
must always implement this method, but if you don't want to allow binding,
then you should return null.
|
|
onUnbind()
|
The system calls this method when
all clients have disconnected from a particular interface published by the
service.
|
|
onRebind()
|
The system calls this method when
new clients have connected to the service, after it had previously been
notified that all had disconnected in its onUnbind(Intent).
|
|
onCreate()
|
The system calls this method when
the service is first created using onStartCommand() or onBind().
This call is required to perform one-time setup.
|
|
onDestroy()
|
The system calls 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, receivers, etc.
|



No comments:
Post a Comment