【问题标题】:How to leave the program successfully如何成功退出程序
【发布时间】:2014-10-27 17:43:35
【问题描述】:

今天写了一个程序,运行成功,快完成了。但是我发现当我关闭Socket,BufferedReaderPrintWriter时,程序总是显示ANR。我对此感到困惑。有没有有什么顺序可以关闭程序吗??

@Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            try {
                mPrintWriter.println("192.168.2.131;"+mSocket.getLocalAddress().toString().replace("/", "")+";byebye");
                mPrintWriter.close();
                mBufferedReader.close();
                mSocket.close();
                net_.interrupt(); //This is the thread to send and receive data.
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

这是我的问题,我想消除这个错误以保持我的程序完美。谢谢

【问题讨论】:

    标签: java android sockets bufferedreader printwriter


    【解决方案1】:

    阅读此Android - how do I investigate an ANR?

    也许可以尝试类似的方法

        @Override
        public void onClick(DialogInterface dialog, int which) {
    
          thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mPrintWriter.println("192.168.2.131;"+mSocket.getLocalAddress().toString().replace("/", "")+";byebye");
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    mPrintWriter.close();
                    mBufferedReader.close();
                }
    
                try{
                    net_.interrupt();
                }catch (IOException e) {
                     e.printStackTrace();
                }finally{
                    mSocket.close();
                }
            }
         });
    
         thread .start();
        }
    

    在您的应用程序结束时,您应该加入该线程

       thread.join();
    

    【讨论】:

    • 成功了。非常感谢。 :D
    猜你喜欢
    • 2020-12-01
    • 2019-05-19
    • 2021-12-09
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多