我有一个 AsyncTask 正在运行,在这个线程中我启动了一个 runOnUiThread 来操作我的视图。如果我关闭我的 AsyncTask,runOnUiThread 在我关闭我的 AsyncTask 后仍然运行 1 倍。如何关闭 runOnUiThread?谢谢!
@Override
protected String doInBackground(String... params) {
while(running.get()) {
// Background thread sleeps
try {
Thread.sleep(produceRadwaste_countdown * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// UI thread touching some view elements
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
radwasteTon.incrementStack();
}
});
// Delete thread if player is game over
if(GameLogic.isGameOver()) {
clearThreads();
}
}
return "Running";
}
public void clearThreads() {
running.set(false);
this.cancel(true);
}