Amirreza zahed
Amirreza zahed

Reputation: 45

How to keep alive a task in background?

I'm using socket.io in my android application. I want to keep the connection alive and never be close, even if close the app. I create a service class like this:

public class MyService extends Service {


private static Socket socket;
{
    try {
        socket = IO.socket("http://192.168.1.52:2500");
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    socket.connect();
    return Service.START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

and in my main class use this code:

    Intent intent = new Intent(this,MyService.class);
    startService(intent);

But Socket not connect!

Upvotes: 1

Views: 415

Answers (1)

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13627

You have to run Service in to keep your connection alive.

public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

      connectSocket()

      return Service.START_STICKY;
  }

   private void connectSocket() {
    try {
        IO.Options options = new IO.Options();
        options.reconnection = true;

        socket = IO.socket("http://192.168.1.52:2500", options);
        socket.on(Socket.EVENT_CONNECT, onConnected);
        socket.on(Socket.EVENT_CONNECT_ERROR, onConnectionError);
        socket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectionTimout);
        socket.on(Socket.EVENT_DISCONNECT, onDisconnect);
        socket.on("Event_Key", eventCallback );
        socket.connect();
    } catch (URISyntaxException e) {
        Log.d(getClass().getSimpleName(), "Socket Connection Exception");
        e.printStackTrace();
    }

  private Emitter.Listener onConnected = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnected");
    }
  };

  private Emitter.Listener onConnectionError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnectionError");
        if (!closeSocket)
            socket.connect();
    }
  };

  private Emitter.Listener onConnectionTimout = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnectionTimout");
        if (!closeSocket)
            socket.connect();
    }
  };

  private Emitter.Listener onDisconnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onDisconnect");
        if (!closeSocket)
            socket.connect();
    }
  };

  private Emitter.Listener eventCallback = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO", "Result: "+ String.valueOf(args[0]);
    }
};

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
}

Service will run in Backgourd until you stop it or system stops it.

Start the service like below:

Intent intent = new Intent(this, MyService.class);
startService(intent);

Upvotes: 1

Related Questions