我不知道如何优雅地解决以下任务:
- 我有几个代码块(操作)要执行。
- 每个块都可以返回
true
以false
指示可以进一步执行。 - 在每个块内部,我必须使用异步方法调用(因为 Android 是完全异步的)。
处理操作示例(现在无法按预期工作):
List<Operation> operations = command.getOperations();
for (Operation operation : operations) {
Log.d(TAG, "Processing operation: " + operation);
OperationResult result = operation.execute(activity);
Log.d(TAG, "Operation result is: " + result);
if (!result.canContinue()) {
break;
}
}
问题是我需要的操作内部,例如,显示 AlertDialog 并等待输入。但是在我调用dialog.show()
我的方法execute
完成后,它返回不正确的结果。
使用 AlertDialog 注册的按钮侦听器示例如下:
final OperationResult result = new OperationResult();
final class ButtonListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int id) {
switch (id) {
case DialogInterface.BUTTON_POSITIVE: {
result.setCanContinue(true);
}
case DialogInterface.BUTTON_NEGATIVE: {
dialog.cancel();
result.setCanContinue(false);
}
}
}
我应该如何修改操作处理以支持 Android 的异步模型?