如何成功地离开程序

时间:2014-09-03 09:21:49

标签: java android sockets bufferedreader printwriter

今天我写了一个程序。它运作成功,差不多完成了。但是当我关闭SocketBufferedReaderPrintWriter时,我发现程序总是显示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();
            }

        }

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

1 个答案:

答案 0 :(得分:0)

阅读此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();