AsyncTask
|
Service
|
|
Purpose
?
|
It
enables proper and easy use of the UI thread. This class allows performing
background operations and publishing results on the UI thread without having
to manipulate threads and/or handlers. It should ideally be used for short
operations (a few seconds at the most.)
|
A
service can run in the background to perform work even while the user is in a
different application
|
It is an
application component?
|
No
|
Yes
|
How to
invoke?
|
The
task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI
thread.
The
task can be executed only once (an exception will be thrown if a second
execution is attempted.)
|
|
How to
stop?
|
A task can be cancelled at any time by invoking cancel(boolean)
After invoking this method, onCancelled(Object),
instead of onPostExecute(Object)
will be invoked after doInBackground(Object[])
returns
|
stopSelf() or stopService(),
the system destroys the service as soon as possible.
|
Is a
thread?
|
Yes
|
No
|
Runs
on UI Thread?
|
Yes
|
Yes
|
Whether
other application can access?
|
No
|
|
Whether
it reduce the risk of ANR
|
No
|
Yes
|
How task
execution happens?
|
The
task can be executed only once (an exception will be thrown if a second
execution is attempted.)
|
If
this service is not already running, it will be instantiated and started
(creating a process for it if needed); if it is running then it remains
running.
|
AsyncTask
Class
Overview
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI thread
without having to manipulate threads and/or handlers.
AsyncTask
is designed to be a helper class around
Thread
and Handler
and does not constitute a generic threading
framework. AsyncTasks should ideally be used for short operations (a few
seconds at the most.) If you need to keep threads running for long periods of
time, it is highly recommended you use the various APIs provided by the java.util.concurrent
pacakge such as Executor
, ThreadPoolExecutor
and FutureTask
.
An
asynchronous task is defined by a computation that runs on a background thread
and whose result is published on the UI thread. An asynchronous task is defined
by 3 generic types, called
Params
, Progress
and Result
, and 4 steps, called onPreExecute
, doInBackground
, onProgressUpdate
andonPostExecute
.
Developer Guides
Usage
AsyncTask
must be subclassed to be used. The subclass will override at least one method (
doInBackground(Params...)
), and
most often will override a second one (onPostExecute(Result)
.)Service
Class Overview
A Service is an
application component representing either an application's desire to perform a
longer-running operation while not interacting with the user or to supply
functionality for other applications to use. Each service class must have a
corresponding declaration in its package'sAndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
Note that services,
like other application objects, run in the main thread of their hosting
process. This means that, if your service is going to do any CPU intensive (such
as MP3 playback) or blocking (such as networking) operations, it should spawn
its own thread in which to do that work. More information on this can be found
in Processes and Threads. The IntentService class is available as a standard
implementation of Service that has its own thread where it schedules its work
to be done.
Developer Guides
What is a Service?
Most
confusion about the Service class actually revolves around what it is not:
·
A Service is not a separate process. The Service object
itself does not imply it is running in its own process; unless otherwise
specified, it runs in the same process as the application it is part of.
·
A Service is not a thread. It is not a means itself to
do work off of the main thread (to avoid Application Not Responding errors).
Thus a
Service itself is actually very simple, providing two main features:
·
A facility for the application to tell the system about something it wants to be doing in the
background (even when the user is not directly interacting with the
application). This corresponds to calls to
Context.startService()
, which
ask the system to schedule work for the service, to be run until the service or
someone else explicitly stop it.
·
A facility for an application to expose some of its
functionality to other applications. This corresponds to calls to
Context.bindService()
, which
allows a long-standing connection to be made to the service in order to
interact with it.
When a
Service component is actually created, for either of these reasons, all that
the system actually does is instantiate the component and call its
onCreate()
and any other appropriate callbacks on the
main thread. It is up to the Service to implement these with the appropriate
behavior, such as creating a secondary thread in which it does its work.
Note
that because Service itself is so simple, you can make your interaction with it
as simple or complicated as you want: from treating it as a local Java object
that you make direct method calls on (as illustrated by Local Service Sample), to providing a full
remoteable interface using AIDL.
Origin: http://developer.android.com
No comments :
Post a Comment