在Android中同步执行多个任务
我不知道如何优雅地解决以下任务:
- 我有几个代码块(操作)要执行。
- 每个块可以返回
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 的异步模型?
I don't know how to elegantly solve the following task:
- I have several blocks of code (operation) to execute.
- Each block can return
true
offalse
to indicate that further execution is possible. - Inside of each block I have to use asyncronous methods calls (Because Android is completeley asynchronous).
Example of processing operations (not working now as expected):
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;
}
}
The problem is that inside of the operation I need, for example, display AlertDialog and wait for the input. But after I call dialog.show()
my method execute
finishes and it returns incorrect result.
Example of the button listener, registerd with AlertDialog is below:
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);
}
}
}
How should I modify processing of operations to support asynchronous model of Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要完成的处理量在毫秒范围内,您可以将代码重组为状态机 - 枚举子任务的列表和跟踪当前或下一个要执行的任务的变量。本质上,当事件发生时,您将完成待办事项列表,直到到达需要异步操作结果的任务(状态)。此时,您启动异步操作并返回。当您从用户输入或其他方式收到事件调用时,您记录数据,然后从保存的状态号恢复执行状态机,直到它再次遇到异步操作并返回。但这仅在您花费的处理时间以毫秒为单位时才有效,否则您会表现迟缓且反应迟钝,在极端情况下会触发 ANR。
如果需要执行更多处理,则应在后台线程或服务中执行此操作,该服务将消息发送到 UI 线程以启动异步事件,然后等待 UI 线程使用可转换为事件的事件进行调用。所需的结果,但同时让 UI 线程自由地响应不相关的事件。
If the amount of processing to be done is in the milliseconds range, you could restructure your code as a state machine - a list of enumerated subtasks and a variable that keeps track of the current or next task to be executed. Essentially, when an event comes in you will work through the to-do list until you get to a task (a state) that requires the result of an asynchronous operation. At that point you launch the asynchronous operation and return. When you get an event call from user input or whatever, you record the data and then resume executing the state machine from the saved state number until it again hits an asynchronous operation and you return. But this only works if the time you spend processing is measured in milliseconds, otherwise you act laggy and unresponsive, triggering ANR in extreme cases.
If you need to do more processing, you should do it in a background thread or a service, which posts messages to the UI thread to launch asynchronous events and then waits for the UI thread to be called with an event that can be translated into the needed result, but meanwhile leaves the UI thread free to respond to unrelated events.
好的,这就是我解决问题的方法:
Ok, here is how I solved the problem: