我有一个线程,在执行若干操作,当用户取消ProgressDialog
时,我希望停止它。
public void run() {
//operation 1
//operation 2
//operation 3
//operation 4
}
这个线程只会运行一次,所以我无法实现循环来检查线程是否仍在运行。
这是我的进度对话框:
//Wait dialog
m_dlgWaiting = ProgressDialog.show(m_ctxContext,
m_ctxContext.getText(R.string.app_name),
m_ctxContext.getText(R.string.msg_dlg_analyse_pic),
true, //indeterminate
true,
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
m_bRunning = false;
}
});
由于我不知道如何停止线程,所以通过循环对线程的操作进行排序来查看它是否仍在运行是否正确,或者是否有更好的方法?
public void run() {
int op = 0;
while(m_bRunning) {
switch(op) {
case 0 :
//operation 1
break;
case 1 :
//operation 2
break;
case 2 :
//operation 3
break;
case 3 :
//operation 4
break;
}
op++;
}
}
即使使用了这种解决方案,如果线程中有太多的操作,那么对这些操作进行排序可能会很困难。是否有更好的方法来实现这一点?