0

I want to transfer data with WifiDirect.

So I connected two devices with it and opened a SeverSocket on one device. When I want to connect to this ServerSocket from the other device by clicking a button in an activity (there I open a socket with the ip of the group owner, the server) I get: "Failed to open Socket Connection".

When I open an IntentService and execute the code there instead of executing the code in buttenClicked method in the activity it works perfectly.

Why is it not possible to open the Socket in an Activity, but in an IntentService?

nilo
  • 699
  • 7
  • 18

1 Answers1

1

Android doesn't allow you to run long running tasks on the ui thread.. your button click is running on the ui thread while the intent service runs on a separate thread so there serversocket can wait till the client connects but on the ui thread it isn't able to run.

So for future apps whenever u have a task which might halt the thread for a long time do it on a different thread using either intentservice or asynctask or normal thread class of java

user2458228
  • 221
  • 1
  • 5