2017-04-17 34 views
0

我正在使用鍵盤設備,我想無限期地聽按鍵。我在while(true)循環中有一個inputStream.read(),它可以工作,直到我希望輸入停止讀取。此時,我會卡在inputStream.read(),直到輸入其他內容。正確的方法來使輸入流繼續閱讀,直到程序關閉?

try { 

    // Call api and get input stream 
    Call<ResponseBody> call = clockUserAPI.getKeyboardStream(); 
    Response<ResponseBody> response = call.execute(); 
    inputStream = response.body().byteStream(); 

    // Continuously run <<<<<<<<<<<<<<<<< 
    while (keepReading) { 

     // Read from input stream 
     Log.w(TAG, "Reading from input stream"); 
     final byte[] buffer = new byte[256]; 
     int bytesRead = bytesRead = inputStream.read(buffer, 0, buffer.length); 

     // Process response 
     Log.v(TAG, bytesRead + " bytes read. Now precessing"); 
     String fullResponse = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);   
     processResponse(fullResponse); 

     try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); }  

    } catch (Exception e) { 
     e.printStackTrace(); 

     // Sleep for a sec 
     Log.d(TAG, "Keyboard thread interrupted"); 
     try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } 

     // Something happened. Close the input stream 
     if (inputStream != null) { 
      try { 
       Log.v(TAG, "Closing input stream"); 
       inputStream.close(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
      try { 
       Thread.sleep(500); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 
      inputStream = null; 
     } 
    } 
} 

關於輸入流和連續輸入的最佳實踐是什麼?

+0

看起來像轉換爲Java壞的COBOL程序。當文本框失去焦點時,文本輸入結束。 – danny117

+0

當文本框沒有焦點時,我還需要按鍵進行註冊。 –

回答

1

如果我理解正確的,你的問題是停止從InputStream閱讀。您可以使用揮發性布爾變量來停止閱讀:

class PollingRunnable implements Runnable{ 

    private static final String TAG = PollingRunnable.class.getSimpleName(); 

    private InputStream inputStream; 
    private volatile boolean shouldKeepPolling = true; 

    public PollingRunnable(InputStream inputStream) { 
     this.inputStream = inputStream; 
    } 


    public void stopPolling() { 
     shouldKeepPolling = false; 
    } 

    @Override 
    public void run() { 
     while (shouldKeepPolling) { 
      final byte[] buffer = new byte[256]; 
      int bytesRead = 0; 
      try { 
       bytesRead = inputStream.read(buffer, 0, buffer.length); 
       String fullResponse = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8); 
       //Process response 
      } catch (IOException e) { 
       Log.e(TAG, "Exception while polling input stream! ", e); 
      } finally { 
       if(inputStream != null) { 
        try { 
         inputStream.close(); 
        } catch (IOException e1) { 
         Log.e(TAG, "Exception while closing input stream! ", e1); 
        } 
       } 
      } 
     } 
    } 
} 

要停止投票只需使用:

// Supply you input stream here 
PollingRunnable pollingRunnable = new PollingRunnable(inputStream); 
new Thread(pollingRunnable).start(); 

//To stop polling 
pollingRunnable.stopPolling(); 
+0

這有幫助。我在okHttpClient上爲我的rest api設置了一個倒數計時器,並且使keepReading變得不穩定。然後,每當我想停止我的線程時,我所要做的就是使用另一個線程將keepReading設置爲false,並且在下一次read()方法超時時它會看到並退出。謝謝! –

0

最佳做法是在專用線程中執行此操作,以便您可以同時執行其他操作。像

InputStream in = ...; 

public void readStream() { 
    byte[] buf = ...; 
    while (in.read(buf) != -1) { 
    } 
} 

public static class MyReader implements Runnable { 
    public void run() { 
    readStream(); 
    } 
} 

new Thread(new MyReader()).start(); 

的東西在這裏是爲從根本上有利於正是上面所說明的鏈接類:https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/io/DataFetcher.java