Hi all, my application needs to perform some activities in background
(like interacting with the network and check the location and some
other stuff), having or not the main activity visible.
Which is the best way to achieve this? Should I write a service for
each kind of job? Or just one service that performs everything?
I am aware that I will need to launch threads / asynctasks in any case
because the service runs on the same thread of the ui, but maybe
having several services is a more clear and readable structure...
I think the consensus is that AsyncTask is the way to go if what
you're trying to do affects the UI thread eventually. Using a service
to compute location for instance would be over complicated.
On 27 Ago, 23:58, "Maps.Huge.Info (Maps API Guru)" <cor...@gmail.com>
wrote:
> I think the consensus is that AsyncTask is the way to go if what
> you're trying to do affects the UI thread eventually. Using a service
> to compute location for instance would be over complicated.
> -John Coryat
Maybe I didn't explain correctly:
If I need to react to some location change for example, and notify the
user, I can't use _just_ an async thread. I need to to all the logic
in a service and throw for example a notification plus a broadcast
that will update the ui if it is on top.
What I was asking is: if I need to perform more than one task in
background (I mean when the phone screen is off, or when the user is
reading the times website while sitting on the toilet), does it make
any sense to create more than one service? Or should I just create one
service that performs many task like listening on the network as well
as being notified of position changes?
I suppose you'd be better off with a service per service instead of one god service. The reason is that it make the code cleaner - you don't need to manage multiple alarms and network connections in one class. An exception would be a service that just does one kind of thing, but handles a queue of requests. To avoid copy and pasting common code, use a common abstract base class that extends Service.
On 28 Ago, 18:32, Frank Weiss <fewe...@gmail.com> wrote:
> I suppose you'd be better off with a service per service instead of
> one god service. The reason is that it make the code cleaner - you
> don't need to manage multiple alarms and network connections in one
> class. An exception would be a service that just does one kind of
> thing, but handles a queue of requests. To avoid copy and pasting
> common code, use a common abstract base class that extends Service.
Ok, it's what I wanted to do. One thing that I'd like to get better
explained is: I am aware that the service(s) run on the same thread of
the ui, so if I have more than one thread will they share the same
thread?? If they listen to the same broadcast for example, will the
intent be queued and served from one service at the time?
It's just curiosity, I know that the time consuming operations must be
performed in async tasks or threads.
Threads are really just a way of running multiple stacks/program counters in the same address space. It's fair to assume they are time-sliced. Services and Activities are run in a single UI thread (AFAIK) and the precessing model is typical UI thread which calls event methods on them sequentially (they don't really run like classical processes). AsyncTasks run from a thread pool. The first few can run concurrently, but after that the next one can't start until another one is finished. Network connections are also pooled (AFAIK), so that after you open a few, additional ones queue up.
To get workable background processing/networking, you'll probably need to use alarms/saved state, since an app's process can be summarily dismissed any time by the OS when other apps need resources. Although some background processing that takes just seconds you might as well do with an AsyncTask in an activity instead of a service.
Don't expect SDK apps to get any processing done when the device is sleeping or turned off (I'm sure you know that already).
On Sat, Aug 28, 2010 at 7:25 PM, Frank Weiss <fewe...@gmail.com> wrote: > I'm not a total expert on this...
> Threads are really just a way of running multiple stacks/program > counters in the same address space. It's fair to assume they are > time-sliced. Services and Activities are run in a single UI thread > (AFAIK) and the precessing model is typical UI thread which calls > event methods on them sequentially (they don't really run like > classical processes). AsyncTasks run from a thread pool. The first few > can run concurrently, but after that the next one can't start until > another one is finished. Network connections are also pooled (AFAIK), > so that after you open a few, additional ones queue up.
> To get workable background processing/networking, you'll probably need > to use alarms/saved state, since an app's process can be summarily > dismissed any time by the OS when other apps need resources. Although > some background processing that takes just seconds you might as well > do with an AsyncTask in an activity instead of a service.
> Don't expect SDK apps to get any processing done when the device is > sleeping or turned off (I'm sure you know that already).
Thanks a lot. I just needed to know that my direction was the good one.