代码之家  ›  专栏  ›  技术社区  ›  Muhammed Refaat

如何检测(关机输入读取)

  •  1
  • Muhammed Refaat  · 技术社区  · 12 年前

    在我的Android应用程序中,当关闭连接(如打开WiFi)时,我会收到无限数量的日志消息 [cds] shutdowninput in read 这会中断我的应用程序并使其进行大量不必要的输入检查,我正在使用java套接字编程,我尝试通过调用 isInputShutdown() 但什么也没有得到,我就是这么努力的:

    public String getServerResponse() throws Exception{
    
          while(true){
                      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
                      if(xbmc_socket.isInputShutdown()){
                         return "stopped";
                      }else{
                         //continue(and that's what it always doing)
                      }
           }
    }
    
    1 回复  |  直到 12 年前
        1
  •  1
  •   Muhammed Refaat    9 年前

    我的问题是因为我宣布 BufferedReader 作为一个 地方的 变量(如问题中我的代码中所示),这导致连接端的套接字打开,无论是从我这边还是从另一边结束。

    因此,为了克服这个问题,我宣布 缓冲区读取 作为一个 全球的 变量,这使我能够在连接关闭时处理它,例如手动关闭套接字或执行最适合我的应用程序的操作:

    public BufferedReader in;
    public String getServerResponse() throws Exception{
      while(true){
          in = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
          // the rest of the code
       }
    }