Skip to content

Instantly share code, notes, and snippets.

@DeepakRattan
Last active March 10, 2016 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeepakRattan/b9cbcdc5c12eb6e12af0 to your computer and use it in GitHub Desktop.
Save DeepakRattan/b9cbcdc5c12eb6e12af0 to your computer and use it in GitHub Desktop.
How to send Socket object from the service to the activity which starts it?
public class SocketService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
Log.e("Service", "created");
try {
Socket mSocket = IO.socket(Constants.CHAT_SERVER_URL);
Log.e("ValueSocket", String.valueOf(mSocket));
//I want to send mSockets to the activity which will start the Service
} catch (URISyntaxException e) {
e.printStackTrace();
}
super.onCreate();
}
// Event Listeners
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("Socket", "Connected");
}
};
private Emitter.Listener onConnectionError = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("Error", "Error in connecting server");
}
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("Disconnect", "Socket Disconnected");
}
};
@Override
public void onDestroy() {
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment