Android系统杀死我的服务中的serversocket

发布于 2024-11-01 13:54:17 字数 2792 浏览 4 评论 0原文

我正在编写一个示例应用程序来创建 Android 上的服务器和连接到 PC 的客户端。我将服务器套接字放在服务的线程中。一切都很顺利,直到屏幕关闭几分钟后。这可能是 Android 杀死了我的服务器,我试图对我的代码设置一个完整的唤醒锁,它不会再杀死我的服务器,但是,我确实希望屏幕像往常一样关闭。 这是我的代码:

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();
            }
        }

    }

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();
            }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷荒 2024-11-08 13:54:17

有两件事对我们有用:

  • Wi-Fi 锁
  • 将 Wi-Fi 睡眠策略设置为从不。即使程序锁定了 Wi-Fi 无线电,某些设备也会在没有此设置的情况下关闭 Wi-Fi 无线电。

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.
踏月而来 2024-11-08 13:54:17

我发现了问题。那是路由器失去了与Android的连接。我尝试ping它,它说“无法访问”,重新连接wifi后,它可以工作,但过了一会儿,它又出现了

I found the problem. That is the router lost the connection to Android. I've tried to ping it and it said "unreachable", after re connect to wifi, it works, but after a while, it comes again

萤火眠眠 2024-11-08 13:54:17

还要尝试保留 WakeLock。两者都对我有用。

Also try to keep WakeLock. Doing both works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文