在Android中同步执行多个任务

发布于 2024-09-24 22:47:50 字数 1265 浏览 5 评论 0原文

我不知道如何优雅地解决以下任务:

  1. 我有几个代码块(操作)要执行。
  2. 每个块可以返回 truefalse 以指示可以进一步执行。
  3. 在每个块内部我必须使用异步方法调用(因为 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:

  1. I have several blocks of code (operation) to execute.
  2. Each block can return true of false to indicate that further execution is possible.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2024-10-01 22:47:50

如果要完成的处理量在毫秒范围内,您可以将代码重组为状态机 - 枚举子任务的列表和跟踪当前或下一个要执行的任务的变量。本质上,当事件发生时,您将完成待办事项列表,直到到达需要异步操作结果的任务(状态)。此时,您启动异步操作并返回。当您从用户输入或其他方式收到事件调用时,您记录数据,然后从保存的状态号恢复执行状态机,直到它再次遇到异步操作并返回。但这仅在您花费的处理时间以毫秒为单位时才有效,否则您会表现迟缓且反应迟钝,在极端情况下会触发 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.

洋洋洒洒 2024-10-01 22:47:50

好的,这就是我解决问题的方法:

  1. 我创建了执行上下文,该上下文在操作之间共享。
  2. 每个操作都可以使用适当的方法调用下一个操作、停止进一步处理等。
  3. 每个操作可以是后台服务,也可以是 Activity。
  4. 完成所有操作后,调用者将通过回调收到通知。
  5. 由于操作也可以是异步的,因此它们使用通知来了解操作何时完成以及应该进行进一步的处理。

Ok, here is how I solved the problem:

  1. I've Created context of execution, which is shared between operations.
  2. Each operation can invoke next operation, stop further processing, etc. using appropriate methods.
  3. Each operation can be either background service, either Activity.
  4. After completion of all operations caller is notified via callback.
  5. Since operations also can be asynchronous, they using notifications to know when operation is completed and further processing should be done.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文