I think his use of the main Looper is OK -- though not something I would endorse, in part, because: I think the problem is in the lifetime of his handlers.
Yidongsoft: Normally, you allocate the Handler in your main thread -- and then HANG ONTO THEM, generally in an instance field of your Activity or Service. They'll pick up the main looper automatically if you create them in the main thread. Now, I don't know for sure that a Handler doesn't install some strong reference to itself in the Looper. But I would expect it to only install a weak reference -- that means, the GC will throw away the Handler -- and its code -- sometime after you exit each of your methods. If this is hypothesis is correct, the reason you see different behavior is likely that the HTC devices you're considering (such as the Nexus One) have more RAM than the Moto DROID, which only has 256 MB. So things get GC'd quicker. I'm not going to go read the code to find out, because there's a pattern I see no need to deviate from: * Create your Handler in the context of whatever you want to associate with handling it. For example, if you're updating a view in an activity, create it in the onCreate() method for the view. * Save your handler in an instance variable in that context. In other words, the Handler is owned by the context in which it runs, not the context in which messages are posted. * When you run some code in some other context that wants to post back -- pass it the Handler, so it can do so. (Often this will be from code from the same class -- you can just refer to the instance variable). Try changing your code to work that way, and see if it works better. I'm not going to look, because I base this guess on long experience designing frameworks like this. And it's how I would do it. You WANT your posted code to go away, if the Handler is no longer needed before it gets to run. Actually, I'd be even MORE aggressive on the point, and not give you any direct access to the Handler, and make you queue it up via the object with which the Handler is associated -- e.g. an Activity or Service. Even if I'm wrong -- you don't want to write your code that way -- handing important information to an object for later use, and then just letting go of it. If coding that way doesn't get you into trouble here, it will somewhere else. And even if it doesn't get you into trouble directly -- when you get in trouble with something else, you'll look at this code, and NOT KNOW whether you have this problem or not. So regardless of whether this is your bug or not -- it's Bad Practice. Or to put a more positive spin on things: It's Good Practice to associate a Handler with the context in which things are being Handled, and initialize them with that object, in the same thread. On Feb 20, 7:10 am, Mark Murphy <[email protected]> wrote: > yidongsoft wrote: > > My code to obtain GPS coordinate works perfect in HTC cells. But I > > find it doesn't work in Moto Droid. I debug it and find eveything > > seems fine. But the coordinate never obtained. > > <snip> > > > No error message, No fatal return. Every things invoked well, but > > onLocationChanged() never be run, getLastKnownLocation() always return > > null ----- No GPS information get at all. I check all the settings. > > GPS device is opened and every thing is OK. The same code works in my > > HTC magic, G2. > > I can tell you that this code works on a DROID: > > http://github.com/commonsguy/cw-android/tree/master/Internet/Weather/ > > as does this code: > > http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/ > > and this code: > > http://github.com/commonsguy/cw-advandroid/tree/master/WebView/GeoWeb1/ > > and this code: > > http://github.com/commonsguy/cw-advandroid/tree/master/WebView/GeoWeb2/ > > In terms of where things may be going wrong in your code, it is > difficult to say. I have managed to write a whole lot of Android code > without having to touch a Looper, so I'd start there. > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://twitter.com/commonsguy > > Android App Developer Training:http://commonsware.com/training -- 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

