如何处理 IntentService 内部的多个异步

How to handle multiple asynchronous inside of IntentService

我正在编写一个应用程序,用于从多个来源多次下载 JSON 数据。每个下载都是使用回调方法异步完成的。

我的想法是使用 IntentService,因为我希望所有数据处理都在同一线程上 运行,并同时调用所有异步下载。但是由于 IntentService 在完成 运行ning onHandleIntent 后停止,所以我怎样才能让它等待所有异步下载完成?这就是我的 onHandleIntent 方法的样子

  @Override
  protected void onHandleIntent(Intent intent) {
       asyncTaskAWithCallback{
            //some handling of data
       }
       asyncTaskBWithCallback{
            //some handling of data
       }

       ...

       asyncTaskNWithCallback{
            //some handling of data
       }
       //Some code to wait for all callbacks to finish?
  }

添加一个循环等待计数器达到异步计数的值 并让每个异步增加这个计数 onPost

我最终创建了一个 Service 而不是 IntentService 就像 CommonWare 在评论中建议的那样。按照此 example 并将数据处理放在单独的线程中。