Improved runnable

This commit is contained in:
PeratX 2017-04-21 23:05:16 +08:00
parent 34ae1e2990
commit eee6d3923f

View File

@ -41,8 +41,8 @@ public class ServerTestActivity extends AppCompatActivity {
private static final String TAG = "DServerTest"; private static final String TAG = "DServerTest";
private static boolean testing = false;
private static Thread mThread = null; private static Thread mThread = null;
private static Runnable mRunnable = null;
private ServerTestHandler mHandler = null; private ServerTestHandler mHandler = null;
@Override @Override
@ -63,21 +63,7 @@ public class ServerTestActivity extends AppCompatActivity {
final Context context = this; final Context context = this;
final Button startTestBut = (Button) findViewById(R.id.button_start_test); mRunnable = new Runnable() {
startTestBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, R.string.notice_start_test, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startTestBut.setVisibility(View.INVISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
textViewTestInfo.setText("");
if (mThread == null) {
Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -140,7 +126,22 @@ public class ServerTestActivity extends AppCompatActivity {
return testText; return testText;
} }
}; };
mThread = new Thread(runnable);
final Button startTestBut = (Button) findViewById(R.id.button_start_test);
startTestBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, R.string.notice_start_test, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startTestBut.setVisibility(View.INVISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
textViewTestInfo.setText("");
if (mThread == null) {
mThread = new Thread(mRunnable);
mThread.start(); mThread.start();
} }
} }
@ -155,11 +156,20 @@ public class ServerTestActivity extends AppCompatActivity {
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
stopThread();
mRunnable = null;
mHandler = null;
}
private static void stopThread() {
try {
if (mThread != null) { if (mThread != null) {
mThread.interrupt(); mThread.join(1);
mThread = null; mThread = null;
} }
mHandler = null; } catch (Exception e) {
Log.e(TAG, e.toString());
}
} }
public static class ServerTestHandler extends Handler { public static class ServerTestHandler extends Handler {
@ -179,20 +189,11 @@ public class ServerTestActivity extends AppCompatActivity {
textViewTestInfo.setText((String) msg.obj); textViewTestInfo.setText((String) msg.obj);
break; break;
case MSG_TEST_DONE: case MSG_TEST_DONE:
testing = false;
startTestBtn.setVisibility(View.VISIBLE); startTestBtn.setVisibility(View.VISIBLE);
mThread = null;
stopThread();
break; break;
} }
} }
} }
@Override
protected void onRestart() {
super.onRestart();
if (testing) {
final Button startTestBut = (Button) findViewById(R.id.button_start_test);
startTestBut.setVisibility(View.INVISIBLE);
}
}
} }