I would agree, the best thing for this would be a service that you can bind do and do AIDL calls through, along with coordinating other things. Passing the raw socket can be accomplished through the application object, though that sounds fairly dirty to me. This is a fairly common use case (in chat clients, etc.., although you theoretically shouldn't be this bandwidth heavy..), and one which I believe is typically implemented via services.
On Mon, Apr 16, 2012 at 12:31 PM, Tonez <apires...@gmail.com> wrote: > Hi Everyone, > > I'm building an Android app which uses TCP sockets to communicate with > a .net server application. The android app as a whole relies quite > heavily on TCP and as such nearly all the features in the app require > writing to and listening from a socket stream. I'm trying to > determine what the best design approach is for having more than one > activity utilize a live active socket. > > I've recently just finished building an iPhone version of this app, > the way in which I got each feature (different view controllers) to > use one live active socket connection was by passing the live socket > instance to each view controller, each view controller would then > retain ownership of that socket and as such the delegate methods which > fire when a transmission is received work as expected. Trying to > simulate this design in Android is proving to be a pain because I > can't pass a live socket instance to another activity as part of an > intent parameter. > > If I wanted to have activity A listen for incoming TCP data, and then > navigate to Activity B but then have activity B send TCP data to > the .net server and of-course spawn a new thread to listen for > incoming TCP data - what would be the best approach to achieve this? > > At the moment what I have is as follows: activity A spawns a new > thread listening for incoming TCP data, activity A can communicate > with the .net server perfectly fine. When I navigate to activity B > and then want to communicate with the .net server - creating a new > socket instance and then listening for incoming data results in > activity A's readLine() method receiving the data. Which makes sense, > it's still running - but obviously the goal is to have activity B > receive this data. > > An alternative approach I tried was to close down the TCP socket I > have in activity A when opening up another TCP socket connection when > I need to use TCP in activity B - although this somewhat works it > really feels like the wrong way to go about it. > > And lastly, one other approach I've thought of is to have one activity > handling all TCP comms with the .net server and contain all the > functionality in this one activity by swapping out .xml layout files > when necessary. Obviously this will result in one massive .java file > and again is a route which feels wrong. > > Any advice on how I can go about designing my app given that I want to > use TCP functionality in every activity would be greatly appreciated. > > Many thanks, > > Tonez > > -- > 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 > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- 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 android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en