Question

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

Was it helpful?

Solution

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().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top