开发者

Not able to close input stream

开发者 https://www.devze.com 2023-03-09 12:06 出处:网络
in this application i am running a remote service where i am starting a separate thread to run TCP connection... on application exit, i have to close the input stream for which i am giving a thread in

in this application i am running a remote service where i am starting a separate thread to run TCP connection... on application exit, i have to close the input stream for which i am giving a thread interrupt and changing a boolean value (stopBroadcastRequested) .. when that is satisfied in the thread block i am closing the inputstream, but it does not get closed actually... if i manually give the instream.close within the thread during normal thread execution, the input stream closes as expected.. but in the if(stopBroadcastRequested){ block of code}, it does not work..can someone tell me what is the mistake i have made...

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(2028开发者_开发百科8): 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


You can use close() only once. Just add a check if (inStream != null { inStream.close(); ) every time.


You get an exception (InterruptedException) at line

Thread.sleep(3000L);

This happens before you even assign a value to the inStream variable, so

inStream = broadCastComm.getCurrentInputStream();

is never executed. You can't close a Socket which is null.


Sorry guys, the issue was that i was not unbinding the service properly which was causing all these issue... was not using the proper context for unbinding within the tabactivity.. had to use getApplicationContext() instead... :)

0

精彩评论

暂无评论...
验证码 换一张
取 消