无法关闭输入流

时间:2011-05-30 12:08:54

标签: android sockets tcp inputstream

在这个应用程序中我正在运行一个远程服务,我正在启动一个单独的线程来运行TCP连接...在应用程序退出时,我必须关闭输入流,我给它一个线程中断并更改一个布尔值(stopBroadcastRequested)..当在线程块中满足我关闭输入流,但它实际上没有关闭...如果我在正常线程执行期间手动给出线程内的instream.close,输入流关闭为预期..但在if(stopBroadcastRequested){代码块}中,它不起作用。有人告诉我我犯了什么错误......

public class BroadcastService extends Service {

    class Task implements Runnable{
    OutputStream outStream = null;
    InputStream inStream = null;

    @Override
    public void run() {
        while(!stopBroadcastRequested){
            Log.i(TAG, "Thread Task started");          
            try {
                isSocketOpen = broadCastComm.isAliveOrOpenSocket("192.168.43.2", 6000, 17, 0);

                if(isSocketOpen){
                    Log.d("SERVICE CLASS", "STARTED THREAD - Writing in output stream");

                    notificationMngr.cancelAll();
                    isShowingNotification = false;
                    outStream = broadCastComm.getCurrentOutputStream();
                    outStream.write(messageToBeSent);
                    if(Integer.valueOf(messageToBeSent[2]) != (byte)0xA0){
                        Log.e("REVERTING", "REVERTING");
                        messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA");
                    }

                    Log.d("OUTPUT STREAM", "Message sent ->" + ByteArrayToString(messageToBeSent));
                }else{
                    connectivityStatusHandler.sendEmptyMessage(0);
                }

                Thread.sleep(3000L);

                if(isSocketOpen){

                }

            } catch (Throwable t) { 
                Log.e(TAG, "Failed to retrieve data in thread", t);
            }
            Log.d("SERVICE CLASS", "End of THREAD");

        }

        if(stopBroadcastRequested){
            Log.e("SERVICE", "STOPPED THREAD");
            try {
                Log.e("*****THREAD", "CLOSED INPUT STARTED");
                if(inStream != null)
                    inStream.close();

                Log.e("*****THREAD", "CLOSED INPUT CLOSED");
                outStream.flush();
                outStream.close();
                Log.e("*****THREAD", "CLOSED OUTPUT");
            } catch (Exception e) {
                Log.e("THREAD", "FAILED TO CLOSE STREAMS");
            }
        }
    }

    public synchronized void stopThread(){
        stopBroadcastRequested = true;
    }

}


@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "Service destroying");
    stopBroadcastRequested = true;
    serviceThread.interrupt();


    cleanNotifications();
    broadCastComm.clearConnections();
    //dbHandler.removeCallbacks(dbUpdater);
    try {
        dbHelper.cleanup();
    } catch (Exception e) {
        Log.e("SERVICE", "Failed to clear DB connections");
    }
}

}

  ------LOG
05-30 19:28:03.878: ERROR/BroadcastService(20288): Failed to retrieve data in thread
05-30 19:28:03.878: ERROR/BroadcastService(20288): java.lang.InterruptedException
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.VMThread.sleep(Native Method)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.sleep(Thread.java:1213)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.sleep(Thread.java:1195)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at      com.RBEI.TTApp.BroadcastService$Task.run(BroadcastService.java:126)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.run(Thread.java:1019)
05-30 19:28:03.878: DEBUG/SERVICE CLASS(20288): End of THREAD
05-30 19:28:03.878: ERROR/SERVICE(20288): STOPPED THREAD
05-30 19:28:03.878: ERROR/*****THREAD(20288): CLOSED INPUT STARTED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED INPUT CLOSED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED OUTPUT

3 个答案:

答案 0 :(得分:1)

您只能使用close()一次。只需添加支票if (inStream != null { inStream.close(); ) 每次。

答案 1 :(得分:0)

您在第

行收到异常(InterruptedException)
Thread.sleep(3000L);

在您为inStream变量赋值之前会发生这种情况, 所以

inStream = broadCastComm.getCurrentInputStream();

永远不会执行。你不能关闭一个null的Socket。

答案 2 :(得分:0)

对不起家伙,问题是我没有正确解开服务,这导致了所有这些问题......没有使用正确的上下文来解除弹性中的绑定..不得不使用getApplicationContext()代替......: )