What do you mean subsequent runs? You should only use a thread/looper once; after that let go of it, and the next time you need a thread make a new one with its own looper.
When you are inside of handleMessage(), Looper.myLooper() and this.getLooper() should be the exact same thing. If they aren't, something is seriously wrong. I would strongly recommend using HandlerThread, and when you want it go go away just call HandlerThread.getLooper().quit(). That should work fine. If you don't want to do that, at least have a look at its code to make sure you are doing something similar since the raw Looper API is unfortunately not documented much at all: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/os/HandlerThread.java On Fri, Jun 12, 2009 at 4:05 PM, Brad Gies <[email protected]> wrote: > > Marco, > > Thanks… I had tried what you suggested early in the process, and it wasn’t > working. But, having you mention it, spurred me to try it again a couple of > different ways, and I finally found something that worked, although I don’t > understand exactly how or why it works ☺. > > Below is what I finally came up with : > > mHandler = new Handler() > { > public void handleMessage(Message msg) > { > if (msg.what == MESSAGE_DONE_WITH_GPS) > { > Cleanup(); > > if (Looper.myLooper() != null) > Looper.myLooper().quit(); > if (this.getLooper() != null) > this.getLooper().quit(); > return; // not sure this is really needed, but it > doesn't seem to hurt. > } > > } > }; > > > The cleanup procedure just makes sure all my variables are cleared and > nulled. > > If someone could explain to me why this works I would really appreciate it > :). > > As background, I created an activity that just has a button that registers > with the AlarmManager, and the AlarmManager calls a WakeWorkManager (got > that from Mark's book) that gets a partial wake lock and then starts the > service, and releases it's own partial wake lock. The service also obtains a > partial wake lock when it starts, and then it creates my main thread. The > main thread checks to see if I need to get a new location and starts the > Looper thread which registers a LocationListener, and then shuts down once > the new location is in the "getLastKnownLocation" method. The main thread > then does other work and shuts down itself, and the service also shuts down > when it is finished. > > The first time the service is created the following works and shuts down > the looper: > > if (Looper.myLooper() != null) > Looper.myLooper().quit(); > > BUT... after that it never works again. > > For all the subsequent runs of the service the following works : > > if (this.getLooper() != null) > this.getLooper().quit(); > > I'm assuming there is some subtle difference in the way the AlarmManager > calls the service the first time, and the way it does it the second and > every time after that. I think it might be that the intent it uses seems to > be different. I haven't explored this enough to know yet, but I have noticed > that there seem to be a lot more null objects/fields in the intent that > comes into the WakeWorkManager the second time it's called than there are > the first time. > > If anyone can clear this up, and explain it to me, I would like really like > to completely understand it. > > Thanks. > > > > Sincerely, > > Brad Gies > > > ----------------------------------------------------------------- > Brad Gies > 27415 Greenfield Rd, # 2, > Southfield, MI, USA > 48076 > www.bgies.com www.truckerphone.com > www.EDI-Easy.com www.EDI-Simple.com > ----------------------------------------------------------------- > > Moderation in everything, including abstinence > ________________________________________ > From: [email protected] [mailto: > [email protected]] On Behalf Of Marco Nelissen > Sent: Thursday, June 11, 2009 11:00 PM > To: [email protected] > Subject: [android-developers] Re: LooperThread > > Send a message to it and have it call Looper.quit() in response. > > On Thu, Jun 11, 2009 at 6:02 PM, Brad Gies <[email protected]> wrote: > > > Can anyone tell me the proper way to terminate a LooperThread? > > Below is the code from the Developer site, but it seems all the methods for > stopping it have been deprecated. > > > > class LooperThread extends Thread { > public Handler mHandler; > > public void run() { > Looper.prepare(); > > mHandler = new Handler() { > public void handleMessage(Message msg) { > // process incoming messages here > } > }; > > Looper.loop(); > } > } > > > I'm using this in a Service, which creates a main Thread, and the main > thread checks to see if I need to update my server with GPS coordinates. If > I do need to send the GPS info, I create a looper thread using the base > code > above, and join to it for a maximum of 60 seconds. > > I am able to register to receive the GPS information, and set a > LocationListener inside the Looper Thread, and receive the GPS information. > When I get the onStatusChanged event with a status of 2 (Available), I want > to stop/terminate the looperThread, so that I can continue on with the main > thread, but I don't see any methods left that allow me to terminate it > properly. The join times out, and the main thread continues, but the > LooperThread never disappears. > > Should I be using another method with SDK 1.5.r2 ? > > > > > > Sincerely, > > Brad Gies > > > ----------------------------------------------------------------- > Brad Gies > 27415 Greenfield Rd, # 2, > Southfield, MI, USA > 48076 > www.bgies.com www.truckerphone.com > www.EDI-Easy.com www.EDI-Simple.com > ----------------------------------------------------------------- > > Moderation in everything, including abstinence > > > > > > > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. 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 [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 -~----------~----~----~----~------~----~------~--~---

