Search code examples
androidsystemserversocketwakelock

Android System kill my serversocket in service


I'm writing an sample app to create a Server on Android and a client to connect to PC. I put the serversocket in a thread of a service. Everything goes perfectly, until a few minutes after the screen goes off. This may be Android kill my server, I tried to put a full wake lock to my code and it wont kill anymore, however, I DO want the screen go off as usual. Here is my code:

public class MessageListener extends Service {

    private ServerSocket serverSocket;

    @Override
    public void onCreate() {
        Log.v("Test", "Create service");
    }

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


         PowerManager.WakeLock wl=null;
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
         wl.acquire();
        startServer();
        if(wl!=null) wl.release();

        return START_STICKY;
    }

    private Runnable thread = new Runnable() {

        @Override
        public synchronized void run() {
            try {



                serverSocket = new ServerSocket(Integer.parseInt(5000));
                ObjectInputStream in = null;

                while (true) {

                    Socket client = serverSocket.accept();

                    Log.v("TCP", "S: Receiving...");
                    try {
                        in = new ObjectInputStream(client.getInputStream());

                        DataInController data = new DataInController(
                                getApplicationContext());
                        data.processDataIn(in.readObject(), client);

                    } catch (ClassNotFoundException e) {
                        System.out.println("TCP S: Error in PC Server Listener");
                        e.printStackTrace();

                    } finally {
                         client.close();    
                    }
                }

            } catch (IOException e) {
            }

        }

    };
    private Thread serverThread;

    private synchronized void startServer() {
        if (serverThread == null) {
            serverThread = new Thread(thread);
            serverThread.start();
        }
    }

    private synchronized void stopServer() {
        if(serverThread!=null){
            Thread t=serverThread;
            serverThread=null;
            t.interrupt();
        }

    }


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

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("TCP", "Killing Service!!!!!!!!!!!!!!!!!!!!!!!");
        if (serverSocket != null) {
            try {
                serverSocket.close();
                stopServer();
                Log.v("TCP", "Closed server socket");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

Solution

  • Two things that worked for us:

    • Wi-Fi lock
    • Set the Wi-Fi sleep policy to never. Some devices will power down the Wi-Fi radio without this setting, even when a program has a lock on the Wi-Fi radio.