I’m writing a simple application that uses sockets. I have an activity that launches a service after a button press; in short, onStart in this service does something like this:
ss=new ServerSocket(portNum);
Socket socket=ss.accept();
//some other unrelated stuff
I though that services can run in background independent from activities (I’m new to android and, in fact, this is my first app), but my activity freezes until ss.accept() is done (I mean, until the client connects – the button remains in pressed state and I can’t do anything). Is this behaviour normal or should I look for an error somewhere in my code? Or maybe put this piece of code in a separate thread?
By default, in Android local services use the same UI thread. That’s why your activity freezes. You should create service, make a thread in this service and run your command in another thread.