1

How to make my app to keep connection on Socket server and to run on foreground and background. I need to receive some notification and data from Server. Until now my app work only when is open, and when i close or only back the connection stops.

And When app is closed and user run app again and try to connect to server again connection don't happen, then I need to restart the server and connect again.

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            iSocketInterface = ISocketInterface.Stub.asInterface(iBinder);
            socketServiceBound = true;

        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

            iSocketInterface = null;
            socketServiceBound = false;

        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, SocketService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    protected void onRestart(){
        super.onRestart();

    }

    protected void onResume(){
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    protected void onStop() {
                super.onStop();
}
Andry Jhonas
  • 379
  • 2
  • 13
  • You mean you want to keep your Background Service running and keep active whether application is active or not. Correct? – Ajay Mehta Apr 12 '19 at 13:12
  • Possible duplicate of [How can we prevent a Service from being killed by OS?](https://stackoverflow.com/questions/9696861/how-can-we-prevent-a-service-from-being-killed-by-os) – Santanu Sur Apr 12 '19 at 13:12
  • @Ajay-Rlogical Yes, and if app is closed, and user run app again and try to connect to server again connection don't happen, then I need to restart the server and connect again. – Andry Jhonas Apr 12 '19 at 13:33
  • 1
    You can keep your background service active whether application is active or closed. You need to start your service as a foreground. Please check my answer here https://stackoverflow.com/questions/55571182/service-stops-working-when-app-gets-closed/55571560#55571560. It might help you. You can also download the sample code to keep your service active while application closed here: https://filebin.net/p5jv54ow5vl4y4gt/ServiceSample.zip?t=p6vqlepa – Ajay Mehta Apr 12 '19 at 13:51
  • @Ajay-Rlogical I start with foreground and make service to run when app is closed, but i don't receive notification. Only I manage to receive notification only when my app is in tasks (before I only receive notification when app was active). Is it Socket connection can cause that problem (like socket connection fails when app is closed) i don't know :( – Andry Jhonas Apr 13 '19 at 07:57
  • @Ajay-Rlogical Can you check my code sample. I have PHP Socket server and client android app. I have tested on Android 4 to 8. It works fine but i think i have some problems with connection. I just need client to listen on incoming messages or notification from server. And i can't connect more than one device to server, either if i stop the app I need to restart the server so I can connect again. Please can you suggest where I make mistake, how to fix this. I just started using java :) https://filebin.net/5qnyuat9001wq7ax – Andry Jhonas Apr 13 '19 at 10:44

0 Answers0