On Wed, Jun 24, 2009 at 1:44 PM, Richard Schilling < [email protected]> wrote: (...)
> How does Looper know what handler class to send messages to? And, where does it get its messages from in the first place? When I have a The message usually comes from Handler.obtainMessage(), and will have its target set to that Handler, so that the Looper knows where to send it. thread that calls Looper.prepare(), then Looper.loop(), then > Looper.exit(), it's not clear how Looper is getting messages for my > class. Looper.loop() looks like it blocks. So, how do I make sure > Looper.loop() doesn't stall my thread waiting for messages? Looper.loop() does "stall". It blocks, because it's what runs the message loop. And, in the case where I want a message handler to process messages in > a different thread, it's not clear how Looper knows what class to send > the messages to. The messages just seem to "come out of the sky" from > somewhere. See above. > > > 3. What do I need to do when I get the "Can't create handler inside > > > > > thread that has not called Looper.prepare()" message? > > > > You call Looper.prepare() before you create the Handler, then some time > > later you call Looper.loop() to start the message handling loop, exactly > as > > done in the sample code at the top ofhttp:// > developer.android.com/reference/android/os/Looper.html > > If you don't want your message processing to be done in that thread, > create > > the Handler somewhere else (your main application thread for example). > > Sounds good - I am envisioning my thread receiving messages somehow. > Perhaps that's my problem here. With Looper.prepare() being called in > my thread, how do I redirect the messages to the handler? How do I > tell my thread to tell Looper where the handler is? Not sure what you mean. You normally don't have to deal with the Looper anymore once it has been created and is running. You just use Handler.obtainMessage() to get a Message, and then Handler.sendMessage() or Message.sendToTarget() to send the message. > 4. How do I use LocationManager.requestLocationUpdates with Looper, > > > > > and why does this call require a Looper object? > > > > Because internally those updates are sent as Messages, so a Handler is > > needed to receive them, so you need a Looper to run the message loop. > > You don't *have* to specify a Looper, but in that case the calling thread > > must have a Looper. For example, you can call the non-Looper method from > > your app's main thread. > > I got around my problem here by calling requestLocationUpdates > (LocationManager.GPS_PROVIDER, 0, 0, locListen, Looper.getMainLooper > ()). What's confusing about this is that I'm expecting locListen to > just have gotten the messages anyway like a normal callback. This > suggests, then, that the application's main message handler is being > used to dispatch messages to locListen. Right? Yes. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

