If you are using HandlerThread, use HandlerThread.getLooper().quit(). If you are using a raw Thread, you should implement some protocol to tell the thread to stop itself.
Generally HandlerThread is a lot easier for this kind of stuff, since it takes care of all of these various details for you. On Oct 4, 3:12 pm, Jo <[EMAIL PROTECTED]> wrote: > HI, > > I got a simple solution for this after some trials. > > instead of creating/starting a new thread for every button click, I > started a thread that listens for button clicks. > > But, can I use thread.stop in onDestroy() to kill the thread. > > Thanks, > --Jo > > On Oct 4, 1:52 pm, Jo <[EMAIL PROTECTED]> wrote: > > > also May I know what is the difference between handlerthread.getid and > > handlerthread.getThreadId. > > > In my application this thread will be created and started everytime a > > button is clicked(for sending chat message). > > so I printed handlerthread.getid and handlerthread.getThreadId. > > > I get incremented values for handlerthread.getid whereas I always get > > -1 for handlerthread.getThreadId.. > > > thanks, > > --Jyothsna > > > On Oct 4, 1:43 pm, Jo <[EMAIL PROTECTED]> wrote: > > > > THanks Justin, > > > I would like to use theHandlerThreadas it sounds simple compared to > > > regular threads. > > > > can somebody post a template for usingHandlerThreadwith > > > android.os.Handler and runnable. > > > > final Handler mHandler2 = new Handler();HandlerThreadandroid_thread_send; > > > // Create runnable for posting > > > final Runnable mUpdateResults2 = new Runnable() { > > > public void run() { > > > updateResultsInUi2(); > > > } > > > }; > > > > and_thread_send = newHandlerThread("sendButtonThread") > > > { > > > > ? > > > ? > > > ? > > > ? > > > > }; > > > > Thanks, > > > --Jyothsna > > > > On Oct 3, 5:07 pm, "Justin (Google Employee)" <[EMAIL PROTECTED]> wrote: > > > > > Someone pointed out that the runThread member should be declared as > > > > volatile or the behavior of this code is unpredictable. > > > > > Cheers, > > > > Justin > > > > Android Team @ Google > > > > > On Oct 3, 4:58 pm, "Justin (Google Employee)" <[EMAIL PROTECTED]> wrote: > > > > > > Jo, > > > > > > Sounds likeHandlerThreadis almost certainly the way you want to go > > > > > here, but you can do a similar thing with a regular thread, using a > > > > > public member to control it. You should stop the the thread in onPause > > > > > or onStop depending on what its doing. > > > > > > public class DummyActivity extends Activity { > > > > > private StoppableThread worker = new StoppableThread(); > > > > > public void onStart(Bundle b) { > > > > > workers.start(); > > > > > } > > > > > > public void onStop() { > > > > > worker.runThread = false; > > > > > } > > > > > > } > > > > > > public class StoppableThread extends Thread() { > > > > > public boolean runThread = true; > > > > > public void run() { > > > > > while (runThread) { > > > > > // do some work > > > > > } > > > > > } > > > > > > } > > > > > > Cheers, > > > > > Justin > > > > > Android Team @ Google > > > > > > On Oct 3, 3:48 pm, Jo <[EMAIL PROTECTED]> wrote: > > > > > > > OK Hackbod you are taking about quittinghandlerThread. Got it. I > > > > > > thought it was normal java.lang.thread. > > > > > > > Thanks, > > > > > > --Jyothsna > > > > > > > On Oct 3, 3:32 pm, Jo <[EMAIL PROTECTED]> wrote: > > > > > > > > HI Hackbod, > > > > > > > > 1) Thanks about the suggestion. I never usedHandlerThread. Maybe I > > > > > > > can take a look. > > > > > > > > 2)coming to the other suggestion.How can I quit a thread. I mean > > > > > > > which > > > > > > > method should I use because thread.resume/suspend/stop are all > > > > > > > deprecated. > > > > > > > > HI Joe, > > > > > > > I am using a separate daemon thread for receiving messages. > > > > > > > Messages > > > > > > > need not be received when chat application is not in use. > > > > > > > So. I am not making it as a service. > > > > > > > > Thanks, > > > > > > > --Jyothsna > > > > > > > > On Oct 3, 5:28 am, Joe Erickson <[EMAIL PROTECTED]> wrote: > > > > > > > > > This almost sounds like it should be a Service. The thread > > > > > > > > will only > > > > > > > > run when you're Activity is running (basically) and isn't > > > > > > > > guaranteed > > > > > > > > to be kept around once the Activity is cleaned up. You may > > > > > > > > want a > > > > > > > > Service running that can handle this and then you'll have much > > > > > > > > more > > > > > > > > control over it. Will you be handing incoming messages the > > > > > > > > same way? > > > > > > > > >http://code.google.com/android/reference/android/app/Service.html > > > > > > > > > On Oct 3, 4:03 am, hackbod <[EMAIL PROTECTED]> wrote: > > > > > > > > > > Just useHandlerThreadto have a single thread sitting there > > > > > > > > > processing messages that you send to it. Even better, create > > > > > > > > > the > > > > > > > > > thread in onStart() and quit it in onStop(). > > > > > > > > > > On Oct 2, 9:49 pm, Jo <[EMAIL PROTECTED]> wrote: > > > > > > > > > > > HI, > > > > > > > > > > > I am developing a chat application. Everytime a user clicks > > > > > > > > > > a button a > > > > > > > > > > thread needs to handle it as text message needs to be sent > > > > > > > > > > over > > > > > > > > > > network. so, I am creating a new thread each time when the > > > > > > > > > > user clicks > > > > > > > > > > on button. I wish only 1 thread handles all the messages > > > > > > > > > > to be sent. > > > > > > > > > > But, resume/suspend/stop are all deprecated. > > > > > > > > > > > is there a way to do it. > > > > > > > > > > > currently, I am doing it like the following. > > > > > > > > > > > <code> > > > > > > > > > > View.OnClickListener showChat = new View.OnClickListener() { > > > > > > > > > > @Override > > > > > > > > > > public void onClick(View v) { > > > > > > > > > > > t_send = new Thread() { > > > > > > > > > > public void run() { > > > > > > > > > > recv = > > > > > > > > > > false; > > > > > > > > > > m_mychat = > > > > > > > > > > doExpensiveSend(); > > > > > > > > > > > > > > > > > > > > > mHandler2.post(mUpdateResults2); > > > > > > > > > > } > > > > > > > > > > }; > > > > > > > > > > t_send.start(); > > > > > > > > > > > } > > > > > > > > > > }; > > > > > > > > > > </code> --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---