1

basically i need a constant socket connected to a server that can be used in multiple activities. at this stage im trying to create and connect the socket on a login screen, where then a new activity starts, sets the input/output streams and then handles the data

im having alot of trouble trying to get the socket to persist in multiple activities. i have read into it and there is a lot of conflicting posts saying that services are better and a singleton is better. nonetheless i choose to try and implement a service. except i cant get it started.

How to keep the android client connected to the server even on activity changes and send data to server?

i first tried to implement the code in the above link but it seems to depend on custom written code called TCPClient. not knowing the internals i moved onto the link below

Android service for TCP Sockets

needless to say ive gotten nowhere in understanding and following the Android resource hasnt really helped. according to the guy who posted the first link his code works, i just dont know whats inside the TCPClient class.

im trying to use the code from the second link, and adapt it for my app, but hasnt worked so far. right now im poking around in the dark.

ultimately, id like an explanation on how to get a tcp socket service started, or a better way to attempt this.

TIA

Community
  • 1
  • 1
  • did you call bindService ? – pskink Nov 27 '14 at 07:36
  • I have, through the doBindService shown in the first link – Chris Bodger Nov 27 '14 at 07:46
  • So i assume your ServiceConnection.onServiceConnected was called? if so, see "service" parameter – pskink Nov 27 '14 at 07:51
  • stepping through the code it doesnt seem the onServiceConnected is being called...what is supposed to call it? – Chris Bodger Nov 27 '14 at 08:27
  • bindService, but not in sync way, read http://developer.android.com/guide/components/bound-services.html carefully – pskink Nov 27 '14 at 08:30
  • i cant seem to find where its not working. its getting called its just not making the assignment heres the code for the second activity after the socket has been created http://pastebin.com/pwscxUMr – Chris Bodger Nov 27 '14 at 09:06
  • so if it's called, what's the problem actually? – pskink Nov 27 '14 at 09:12
  • its working now. might have something to do with my code structure... thanks for all your help :) – Chris Bodger Nov 27 '14 at 09:19
  • ive figured out why i kept on getting socketSevice = null. its not getting a chance to do the binding before the createPlayer() method is called. whats the way around that? – Chris Bodger Nov 27 '14 at 09:55
  • createPlayer is never called, where do you call it? – pskink Nov 27 '14 at 09:56
  • its commented out, line 47 in the pastebin. – Chris Bodger Nov 27 '14 at 10:00
  • i dont understand you, if its commentted out, how it can be called then? – pskink Nov 27 '14 at 10:01
  • here is a new pastebin if it makes it easier for you http://pastebin.com/M0hXnFh2 because ive put it back into the code. while it is back in the code it crashes at line 122 reporting a null error. im assuming that the createPlayer method is being completed before the socketService has a chance to complete. – Chris Bodger Nov 27 '14 at 10:09
  • `onServiceConnected` is called **ASYNCHRONOUSLY**, bindService finishes **without** waiting for `onServiceConnected,` read "bound services" doc – pskink Nov 27 '14 at 10:12
  • quoting the doc: "The bindService() method returns **immediately** without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service." – pskink Nov 27 '14 at 10:16
  • i know you must be getting fed up with me by now, but im sorry i dont know how to work around it... – Chris Bodger Nov 27 '14 at 12:02
  • ok so now you call createPlayer after connection has been made, right? it doesn't work? – pskink Nov 27 '14 at 12:05
  • correct. it throws a nullpointerexception because socketService is null when it tries to get the socket for the object streams (lines 122 and 123) – Chris Bodger Nov 27 '14 at 12:18
  • how can it throw null pointer exception if you use socketService after assignment "socketService = ..." ? does getService() return null? – pskink Nov 27 '14 at 12:23
  • i dont know, 2 theories i have 1. because bindService is asynchronous its still creating the socketService when createPlayer is called, 2. its waits for the end of the onCreate method to finish the rest of it (based on the docs saying it is created without value) this is based on observations and wild guesses... – Chris Bodger Nov 27 '14 at 12:31
  • ok, you have `socketService = ...` in `onServiceConnected`, right? this is line 84 in you pastebin output, then in the very next line call `createPlayer(playerName)` - it will be line 85, i don't know where playerName comes from you need to get it from somewhere, probably from Intent extras as your code says – pskink Nov 27 '14 at 12:44
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/65765/discussion-on-question-by-chris-bodger-trouble-creating-a-socket-service-for-a-m). – Taryn Nov 27 '14 at 12:58

0 Answers0