I suppose by 'being alive' means that the activity has been created
(onCreate has been called) but not yet destroyed (onDestroy has not
yet been called).
The Andriod OS will *not* kill a thread that you started. If you find
that your thread ends prematurely, check if the code in your thread
throws an unhandled exception or that your while loop in your run()
method exit prematurely.
Start your sub-thread when necessary and hold on to it (i.e. assign it
to an instance variable of your activity).
In the 'run()' method of your thread, code a while-loop, e.g.
...
while (!mDone) {
...
// here all your thread's work happens. E.g. reading jobs/requests
from queue and handling them
...
}
..
Then in your onDestroy, wake up the thread just after you set the
'mDone' to true. This will exit the while loop and your thread will
end graciously.
If you don't want to create a thread from scratch that handles queued
jobs/requests, take a look at the java.util.concurrent.ExecutorService
(FutureTask<?>). It takes care of that for you.
Be sure to end a thread in an activity when the activity is about to
be destroyed (also, if your thread has blocking IO, find a way to
interrupt the IO).
On Mar 1, 11:47 pm, Ashrotronics <[email protected]> wrote:
> First thanks for your response.Ye,I start my new Thread from my
> activity, But the new thread does sth very important,and it should
> never be killed by the System as long as my activity is alive ,Any way
> i can get the message when the system killed my thread ??
> On Mar 2, 12:28 pm, Jon Colverson <[email protected]> wrote:
>
>
>
> > On Mar 2, 3:32 am, Ashrotronics <[email protected]> wrote:
>
> > > As is mentioned above,What i'm trying is to protect a sub-
> > > thread,and keep it going on as long as the App that starts it is still
> > > alive.In other words,If the system need more resources,Let it kill
> > > both the App main thread and the sub-thread started from it,Instead of
> > > just kill the sub-thread.Is there any way i can do this ??
> > > And another question here,The android system seems to be "rude",As
> > > it killed my thread without telling me,even in the logs,Is this the
> > > case ?
>
> > Are you starting the thread from an Activity? If you need a thread
> > that continues to run when the Activity is no longer visible, then you
> > should create it from a running Service.
>
> > --
> > Jon- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---