當我的活動創建時,我啓動了一個線程,開始連續按鍵盤按鍵(while(true))
...這可以很好地工作,但是當我點擊返回輸入流未關閉,並且下一次打開該活動響應時零星而不可靠。如何確保我的輸入流在活動退出(Android)上關閉?
我的代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keypad_test_button);
// Set the view pager for return
MainActivity.ViewPagerPosition = 1;
// These are text views that turn green when the corrosponding key is pressed
key0 = (TextView) findViewById(R.id.btn_keypad_0);
key1 = (TextView) findViewById(R.id.btn_keypad_1);
key2 = (TextView) findViewById(R.id.btn_keypad_2);
key3 = (TextView) findViewById(R.id.btn_keypad_3);
key4 = (TextView) findViewById(R.id.btn_keypad_4);
key5 = (TextView) findViewById(R.id.btn_keypad_5);
key6 = (TextView) findViewById(R.id.btn_keypad_6);
key7 = (TextView) findViewById(R.id.btn_keypad_7);
key8 = (TextView) findViewById(R.id.btn_keypad_8);
key9 = (TextView) findViewById(R.id.btn_keypad_9);
keyClear = (TextView) findViewById(R.id.btn_keypad_clear);
keyEnter = (TextView) findViewById(R.id.btn_keypad_enter);
keyF1 = (TextView) findViewById(R.id.btn_keypad_f1);
keyF2 = (TextView) findViewById(R.id.btn_keypad_f2);
keyF3 = (TextView) findViewById(R.id.btn_keypad_f3);
keyF4 = (TextView) findViewById(R.id.btn_keypad_f4);
keyF5 = (TextView) findViewById(R.id.btn_keypad_f5);
keyF6 = (TextView) findViewById(R.id.btn_keypad_f6);
keyF7 = (TextView) findViewById(R.id.btn_keypad_f7);
keyF8 = (TextView) findViewById(R.id.btn_keypad_f8);
// This is for later checking to see if all keys have been pressed at some point
allKeys = new TextView[] { key0, key1, key2, key3, key4, key5, key6, key7, key8, key9,
keyClear, keyEnter, keyF1, keyF2, keyF3, keyF4, keyF5, keyF6, keyF7, keyF8 };
// I eventually set whether all keys have been pressed by looking at their tags
for (TextView button : allKeys) {
button.setTag(0);
}
// I use this method to create a rest api to work with my hardware keypad
createRestAPI();
clockUserAPI.getKeyboardStream().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
if (response == null) Log.v(TAG, "Null response");
else {
Log.v(TAG, "About to start thread");
Thread keyboardInputThread = new Thread(new Runnable() {
@Override
public void run() {
running = true;
InputStream inputStream = null;
try {
// THIS IS WHERE THE INPUT STREAM IS DEFINED
inputStream = response.body().byteStream();
byte[] buffer = new byte[256];
// PRETTY MUCH while(true) ... THIS CONTINUOUSLY LISTENS FOR KEYPAD PRESSES
while (running) {
Log.v(TAG, "Reading...");
int bytesRead = inputStream.read(buffer, 0, buffer.length);
Log.v(TAG, bytesRead + " Bytes read");
String fullResponse = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
Log.v(TAG, "Full response: " + fullResponse);
// I use this method to parse the json response for the key I want
processResponse(fullResponse);
}
} catch (IOException e) {
Log.v(TAG, "Exception...");
e.printStackTrace();
} finally {
// I TRY TO CLOSE THE INPUT STREAM HERE BUT THE PROGRAM NEVER REACHES THIS POINT.
try {
Log.v(TAG, "Closiing input stream...");
inputStream.close();
Log.v(TAG, "Success");
} catch (IOException e1) {
e1.printStackTrace();
Log.v(TAG, "Failure...");
}
}
}
});
keyboardInputThread.start();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "Response error failure: throwable=" + t);
}
});
}
我的理論是,輸入流是永遠不會被關閉,因爲try塊永遠不會結束。它連續運行while(true)
。我需要這樣的情況。那麼當用戶點擊返回時,我在哪裏關閉輸入流?我試過onStop()
和onDestroy()
,但這些也不保證被調用。
@覆蓋 公共無效onBackPressed(){// 你的代碼。 }你嘗試過 –
使用[onPause](https://developer.android.com/guide/components/activities/activity-lifecycle.html#onpause)將用戶離開活動時的運行設置爲false。但是您的閱讀電話在等待輸入時也會被阻止,因此您也必須對此進行說明。 – Steveadoo
@android_jain:關閉輸入流onPause會導致異常導致:java.lang.IllegalStateException:輸入/輸出不平衡 –