我初始化了一个更新进度条的线程,在该线程内部,我有一个for循环来更新进度条, 这部分工作正常,但是...在应用程序上,我有一个取消按钮,可以停止线程(和for循环),
现在我在日志中看到的内容是“ STOP THREAD”,但是进度条不断更新内容;
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
Log.e(TAG, "HERE");
for (int progress = 0; progress <= progressMax; progress+=4096) {
notification.setProgress(progressMax, progress, false);
notificationManager.notify(id, notification.build());
}
notification.setContentText(text)
.setProgress(0, 0, false);
notificationManager.notify(id, notification.build());
}
});
if (value == 2000) {
myThread.start();
} else {
Log.e(TAG, "STOP THREAD");
myThread.interrupt();
notification.setContentText(text)
.setProgress(0, 0, false);
notificationManager.notify(id, notification.build());
}
}
答案 0 :(得分:0)
如果已取消,则应添加一个标记来标记。并在for循环内检查并退出。按下取消按钮时,将标志设置为true。
for (int progress = 0; progress <= progressMax; progress+=4096) {
if(quit_flag) {
// do some clear work
return;
}
notification.setProgress(progressMax, progress, false);
notificationManager.notify(id, notification.build());
}