我有两个Intent服务-IntentServiceAIntentServiceB
它们具有以下类定义:

public class FirstService extends IntentService {
  public FirstService() {
      super("first_service");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
      Log.d("chi6rag", "first starts");
      for (long i = 0l; i < 10000000l; i++) {
          if (i == 0l) Log.d("chi6rag", "first started");
      }
      Log.d("chi6rag", "first ends");
  }
}


public class SecondService extends IntentService {
  public SecondService() {
      super("second_service");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
      Log.d("chi6rag", "second starts");
      for (long i = 0l; i < 10000000l; i++) {
          if (i == 0l) Log.d("chi6rag", "second started");
      }
      Log.d("chi6rag", "second ends");
  }
}

如果我在“Activity ”中执行以下代码:
startService(new Intent(this, IntentServiceA.class));startService(new Intent(this, IntentServiceB.class));
我在日志中看到以下内容:
D/chi6rag (11734): first startsD/chi6rag (11734): first startedD/chi6rag (11734): second startsD/chi6rag (11734): second startedD/chi6rag (11734): first endsD/chi6rag (11734): second ends
虽然可以看到Intent Service Docs,但它显然提到它将one intent at a time传递给IntentService的onHandleIntent方法

问题:每个 Intent 服务都有单独的工作线程吗?因为预期的日志是:
D/chi6rag (11734): first startsD/chi6rag (11734): first startedD/chi6rag (11734): first endsD/chi6rag (11734): second startsD/chi6rag (11734): second startedD/chi6rag (11734): second ends

最佳答案

IntentService执行以下操作:



由于您有多个扩展了IntentService形式的类,每个类都有自己的特定onHandleIntent。这些在单独的工作队列上处理。

参见Extending the Intentservice

10-07 20:54