我有两个意图服务- 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");
}
}
如果在活动中执行以下代码:
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
当你看到意图服务文档时,它清楚地提到它将one intent at a time
传递给IntentService的onHandleIntent
方法
问:每个意图服务是否都有单独的工作线程?因为预期的日志是:
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
发布于 2016-05-25 06:35:21
IntentService执行以下操作:
由于您有多个类,扩展表单IntentService,每个类都有各自的特定onHandleIntent。这些操作在单独的工作队列中处理。
发布于 2016-05-25 06:37:43
看看这些文档:IntentService
所有请求都是在一个工作线程上处理的--它们可能需要尽可能长的时间(并且不会阻塞应用程序的主循环),但是一次只处理一个请求。
AFAIK,所有请求都将在单个线程per IntentService
上处理。这意味着,如果创建了许多IntentService
,就不能保证首先执行IntentService
的线程。
如果您想在单个线程中运行多个线程或按顺序工作,请尝试像在ExecutorService
中那样使用Executors.newSingleThreadExecutor()
和runnable
来播放ExecutorService实例。
https://stackoverflow.com/questions/37429107
复制相似问题