views:

128

answers:

1

I'm pretty sure that android services are going to be the end of me. I have almost no hair left after the last few days....

...anyway, I digress.

At first I was having a heck of a time getting the service to bind on an onclick of a button, got that straightened out from help here yesterday. Now I actually want the service to bind in the onCreate() method of the activity. That part is no problem, works as intended. My service is actually a socket service to connect TCP sockets to a socket server that I wrote. If I put the call to the method from the bound service mBoundService.StartSocketServer() inside a button click, bingo, works great. But I need this to fire up immediately when the activity loads, so directly under my bindService() call within my onCreate() method of the activity. When I place the call to start the socket inside my onCreate() I get a force-close.

This method (StartSocketServer()) spawns a new thread then opens the socket on that thread to my remote machine. I'm guessing that the problem lies with the new thread generation before the activity fully loads...not sure.

LogCat is fairly cryptic here. It does say something about thread attach failed, then shows an uncaught handler exception that has "Caused by: java.lang.NullPointerException" within it.

Again, if I put this call to the method inside a button click, I"m in business, if it's in the onCreate() it fails. Is there some way inside an activity (presuming that my assumption is correct that it needs to fully load before spawning a new thread) to call the StartSocketServer() after it's loaded: ala body.onLoad() in html?

please help me save the rest of my hair :)

TIA

+2  A: 

As I wrote in your earlier question:

FWIW, bindService() is asynchronous. You will not have a bound connection until onServiceConnected() is called on your ServiceConnection.

In your bindService() call, you are supplying a ServiceConnection object. That object will be called with onServiceConnected() when the service is ready for use. Among other things, that is probably where you are populating mService. Put your call to StartSocketServer() in onServiceConnected().

CommonsWare
great that fixed it. And as I said in the previous post that you referenced, I don't quite understand the asynchronous thing. Could you shed some more light on that? Why would it not work after the mBoundService is created (within ServiceConnection->onServiceConnected) by calling mBoundService.StartSocketServer() from onCreate of the activity? At that point isn't the service bound?
Kyle
@Kyle: `bindService()` is asynchronous. When `bindService()` returns, **your service is not bound**. Asynchronous means "doesn't happen right away". Or, to quote Wikipedia, "In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing." Asynchronous operations are common in most modern programming environments.
CommonsWare
I see. Thanks again for the exceptional help.
Kyle