Chi*_*rag 5 multithreading android android-intent android-intentservice
我有两个意向服务- 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");
}
}
Run Code Online (Sandbox Code Playgroud)
和
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");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在“活动”中执行以下代码:
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给onHandleIntentIntentService的方法
问题:每个意图服务是否都有单独的工作线程?因为预期的日志是:
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 执行以下操作:
- 创建一个默认工作线程,该线程独立于应用程序的主线程执行传递给 onStartCommand() 的所有意图。
- 创建一个工作队列,一次将一个意图传递给 onHandleIntent() 实现,因此您永远不必担心
多线程- 提供 onStartCommand() 的默认实现,它将意图发送到工作队列,然后发送到 onHandleIntent() 实现。
由于您有多个从 IntentService 扩展的类,每个类都有自己特定的 onHandleIntent。这些是在单独的工作队列上处理的。
| 归档时间: |
|
| 查看次数: |
341 次 |
| 最近记录: |