Also the question is backwards -- the point of a service is to tell the
system you have some background operation that needs to continue running.
You wouldn't stop the service until your thread is done with its work, so by
definition onDestroy() won't be called until that thread is done and thus
there is no need to wait for it.

On Sat, Jan 24, 2009 at 7:04 AM, Guillaume Perrot
<guillaume.p...@gmail.com>wrote:

>
> If you want to stop a thread that is running long operations, you
> should read this: http://g.oswego.edu/dl/cpj/cancel.html.
> In your service onDestroy you should not "join" your thread, just
> launch the cancel operation, the trick with all lifecycle methods
> (including services) is that they are called in the main thread.
> If you block on the main thread, you won't be able to start a new
> activity from this process (the process is rarely stopped, and
> relaunched automatically when a crash occurs), it will freeze and
> you'll see the "sorry dialog" with the "wait" and "force close"
> buttons.
>
> On Jan 24, 11:01 am, Mariano Kamp <mariano.k...@gmail.com> wrote:
> > And when implementing stuff as a service where would the actual long
> running
> > functionality go?
> > In a separate thread?
> > How should I stop that thread?
> >
> > Is acting on Service.onDestroy() the right time and will it definitively
> be
> > called?
> > How much time do I have in onDestroy()?
> > Is it acceptable behavior to set a flag that the background thread should
> > stop and then wait for the thread signaling that it is finished?
> >
> > To illustrate that, think of a Download Service that should download four
> > files.
> > When onDestroy() is called a flag shouldShutdown is set to true and
> > onDestroy() waits then for the thread to finish (Thread.join()).
> > The thread itself is in the midst of downloading the 2nd file and
> monitors
> > shouldShutdown and doesn't start a new download (if possible doesn't even
> > ask for the next bytes) when it is set, but cleans up resources (open
> > connections) and terminates itself.
> >
> > Is that ok?
> >
> > It would certainly not help the OS to quickly get rid of the service, at
> > least when the service is busy with a "long" lasting operation.
> >
> > On Fri, Jan 23, 2009 at 6:45 PM, Dianne Hackborn <hack...@android.com
> >wrote:
> >
> > > On Fri, Jan 23, 2009 at 7:47 AM, g1bb <corymgibb...@gmail.com> wrote:
> >
> > >> I realized this right after I posted, by running a bunch of other apps
> > >> while my app was still running. It seems like 'setPersistent' on the
> > >> activity should most likely prevent this. Any ideas?
> >
> > > DO NOT DO THAT.
> >
> > > Please read this:http://code.google.com/android/intro/lifecycle.html
> >
> > > Basically you should have things you want to continue running in the
> > > background implemented as a Service.
> >
> > > Also there is generally no need to create a whole new Thread for this
> kind
> > > of stuff, you can just post delayed messages to your own handler.
> >
> > > Finally, for something like a countdown timer, you really might want to
> > > consider using the alarm manager so you don't need to keep your app
> running
> > > at all while it is in the background.  That is the kind of thing a well
> > > behaving Android app will do.  To be able to show the remaining time if
> the
> > > user returns to your activity, you can store on SharedPreferences the
> time
> > > the countdown was started.
> >
> > > Using the alarm manager is also the only way you can make sure you
> execute
> > > when the time expires, even if the user has turned off the phone.
> >
> > >> Thanks again.
> >
> > >> On Jan 23, 8:36 am, Torgny <torgny.bj...@gmail.com> wrote:
> > >> > Your application background process might get killed if you run
> > >> > another application that takes up a lot of memory, for instance the
> > >> > browser with several windows. As far as I understand it, the
> > >> > application in the foreground takes priority as far as memory and
> > >> > processing power goes over services and threads.
> >
> > >> > On Jan 23, 9:15 am, g1bb <corymgibb...@gmail.com> wrote:
> >
> > >> > > Hello,
> >
> > >> > > I've created an app that functions as a countdown timer via a
> thread,
> > >> > > a wakelock, and a handler back to my activity. My question is,
> what
> > >> > > will cause my thread to die, or app to not be running in memory
> > >> > > anymore? I've had this happen once, and can't seem to recreate it
> > >> > > again.
> >
> > >> > > Thanks in advance!- Hide quoted text -
> >
> > >> > - Show quoted text -
> >
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > hack...@android.com
> >
> > > Note: please don't send private questions to me, as I don't have time
> to
> > > provide private support.  All such questions should be posted on public
> > > forums, where I and others can see and answer them.
> >
>


-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to