我已经实现了我自己的android服务如下

public class MyService extends Service {
    private static final String TAG = "MyService";

    private Server mServer;
    private LocalServerSocket server;

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

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        mServer = new Server();
        mServer.start();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        if(server != null){
            try {
                server.close();
            } catch (IOException e) {
                Log.d(TAG, "exception in server close");
                e.printStackTrace();
            }
        }
    }

    @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStart");
        return START_STICKY;
    }

    class Server extends Thread {
    @Override
        public void run() {
             try {
                server = new LocalServerSocket("my.socket");
                while (true) {
                    LocalSocket receiver;
                    try{
                        receiver = server.accept();
                    }catch(SocketException e){
                        Log.d(TAG, "SocketException");
                        break;
                    }
                    catch(IOException e){
                        Log.d(TAG, "IOException");
                        break;
                    }
                    if (receiver != null) {
                        Log.d(TAG, "Got Data in receiver");
                    }
                    receiver.close();
                }
            } catch (IOException e) {
                Log.d(TAG, "one more");
                e.printStackTrace();
            }
        }
    }
}

我面临的问题是,如果我的 LocalServerSocket 在 accept() 中阻塞,那么在 OnDestroy() 中调用 server.close() 将不会抛出 SocketException。因此,下次启动服务时,我会收到“地址已在使用异常”。如果我使用 java.net.Socket 而不是 LocalServerSocket,那么我会得到所需的行为。我想知道为什么 LocalServerSocket 的行为与 Java Sockets 不同。就我而言,我如何摆脱 while 循环。

最佳答案

我遇到了同样的问题并以这种方式“解决”了它。线程 run() 方法正在检查“!isInterrupted()”。我添加到我的 Listener-Thread 的方法“stopSocketServer()”标记了中断()的线程,然后向自身发出连接请求以触发accept()方法。

/**
 * Executed if thread is started.
 */
public void run() {

    try {
        // leave while loop if thread is marked for interrupt.
        while (!isInterrupted()) {
            LocalSocket clientSocket = serverSocket.accept();

            if (!isInterrupted()) {
                threadPool.execute(new ClientProcessor(clientSocket));
            }
        }

    } catch (IOException e) {
        if (!isInterrupted()) {
            Log.e(TAG, "socket listener terminated", e);
        }
    } finally {
        try {
            if (serverSocket != null) {
                serverSocket.close();
            }
            if (threadPool != null) {
                threadPool.shutdownNow();
            }
            Log.i(TAG, "socket listener stopped");
        } catch (IOException e) {
        }
    }
}

public void stopSocketServer() {
    if (serverSocket != null) {
        try {
            // mark thread as interrupted
            interrupt();

            // now send connect request to myself to trigger leaving accept()
            LocalSocket ls = new LocalSocket();
            ls.connect(serverSocket.getLocalSocketAddress());
            ls.close();
        } catch (IOException e) {
            Log.e(TAG, "stopSocketServer failed", e);
        }
    }
}

10-06 14:03