我有两个Intent服务-IntentServiceA
和IntentServiceB
它们具有以下类定义:
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 starts
D/chi6rag (11734): first started
D/chi6rag (11734): second starts
D/chi6rag (11734): second started
D/chi6rag (11734): first ends
D/chi6rag (11734): second ends
虽然可以看到Intent Service Docs,但它显然提到它将
one intent at a time
传递给IntentService的onHandleIntent
方法问题:每个 Intent 服务都有单独的工作线程吗?因为预期的日志是:
D/chi6rag (11734): first starts
D/chi6rag (11734): first started
D/chi6rag (11734): first ends
D/chi6rag (11734): second starts
D/chi6rag (11734): second started
D/chi6rag (11734): second ends
最佳答案
IntentService执行以下操作:
由于您有多个扩展了IntentService形式的类,每个类都有自己的特定onHandleIntent。这些在单独的工作队列上处理。
参见Extending the Intentservice