This question relates to my last question in here Socket.isConnected() make my android app force close and now I can migrate to another Activity
but I don't know to use the Socket
that is connected from the previous Activity
, send and receive response from server too. Please help me guys :(
-
Don't do socket work in your activities. Create a separate class to handle the network activities. Provide access to the socket via static methods of the class. – Simon Jan 27 '15 at 22:36
-
Do you have any reference for me?? Sorry I'm really new here – ginc0de Jan 27 '15 at 22:45
-
developer.android.com – Simon Jan 27 '15 at 22:58
1 Answers
I have posted answer to your previous question about how to properly connect to Socket because AsyncTask
is really bas solution, it will eventually terminate if previous Activity
terminates (I'm not exactly sure when AsyncTasks
are dying exactly). @ginc0de suggested perfect solution. Study something about Singleton for example... I think this is best solution for your problem.
public class ConnectThread extends Thread {
// singleton Part
private static ConnectThread instance;
public static ConnectThread getInstance(){
return (instance == null) ? instance = new ConnectThread() : instance;
}
private ConnectThread(){
}
// implementation part
private Socket mSocket;
public Socket getSocket() {
return mSocket;
}
@Override
public void run() {
mSocket = new Socket();
// connect etc.
}
}
Firstly you have to call ConnectThread.getInstance().start()
somewhere (no more than once to start Thread) Then you should obtain Socket anywhere by calling ConnectThread.getInstance().getSocket()
After completing communication or leaving application (this is hard part, and I really don't think you should handle Socket on multiple Activities) you have to close Thread (interrupt()
) and socket in it (override the interrupt()
method and call Socket.close()
)
Be aware of method Activity.runOnUiThread()
study how interfaces are working (you will have to implement some listeners), don't give up but try to understand basics of Object Oriented Programming (Just in this answer it is Singleton, Interfaces, static variables)

- 3,036
- 1
- 25
- 38
-
-
The methods should be static since you cannot create an instance of the class as the constructor is private. – Simon Jan 28 '15 at 08:14
-
I create one file SocketService.java and give some parameter on my method maybe you can check in here [SocketService.java](https://github.com/ginc0der/socket-service/blob/master/com/example/skripsi/socket/SocketService.java) and you can find main
Activity
in [here](https://github.com/ginc0der/socket-service/blob/master/com/example/skripsi/MyActivity.java) when I try to connect, the `Toast` as notification for the `Thread` is not alive is appeared and when I try to connect again my application will be force close. – ginc0de Jan 28 '15 at 16:38 -
And in `SocketService.java` you can find `Context` as parameter, that because I wanna try to migrate `Activity` from `SocketService.java` but always fail and make my force close. – ginc0de Jan 28 '15 at 16:38
-
We are not here to solve you school project or what is it :) Accept answer, read Logs and Google a little bit. – VizGhar Jan 28 '15 at 20:14