【发布时间】:2013-03-12 11:31:52
【问题描述】:
我有一个线程运行多个操作一次,我想在用户取消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++;
}
}
即使使用此解决方案,如果线程中的操作过多,也可能难以对操作进行排序。有没有更好的方法来实现这一点?
【问题讨论】:
标签: android multithreading progressdialog