Will wrote: > Can you elaborate on your advice Mark? > > How would I put a looper comfortably into a background thread that's > reading from a socket?
I haven't done it before, because I disagree with your pattern -- binding should be between Android components, not random other hunks of code. In terms of how to add a Looper to a thread: http://developer.android.com/reference/android/os/Looper.html The fact that this does not seem like it will work well with your blocking I/O pattern is one of the reasons I haven't used it before. > And what is the local singleton, and how does it help me invoke RPC > with services? You don't invoke "RPC" with a local singleton. My assumption and sincere hope is that the service your thread wishes to communicate with is in your own application and process. In that case, the service is a natural singleton: there are exactly 0 or 1 copies running at any point. The service singleton pattern: Step #1: Somebody, somewhere, calls startService() to start the service up, well in advance of needing it Step #2: In onCreate() of the Service, stick "this" in a static data member on the Service class (either a public one or one accessed via a public static getter) Step #3: When the thread needs to talk to the Service, it gets access to the Service directly via that static data member on the Service class and just calls methods Step #4: In onDestroy() of the Service, null out that static data member, so the Service can get garbage collected Step #5: Do not pass that Service object around outside of that static data member (e.g., don't have the thread cache it locally), lest you get in trouble with garbage collection and reachability -- Mark Murphy (a Commons Guy) http://commonsware.com | http://twitter.com/commonsguy _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.3 Available! -- 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

