Android:多个intentservices或一个具有多个意图的intentservice?

时间:2016-06-13 17:00:37

标签: android multithreading android-service android-intentservice worker-thread

我对intentService有点困惑。文档说如果你发送一个intentService多个任务(意图),那么它将在一个单独的线程上一个接一个地执行它们。我的问题是 - 是否可以同时拥有多个intentService线程?如何区分在同一个intentService(同一个线程)上创建三个不同意图的代码,或三个单独的intentServices,每个都有自己的线程和一个意图执行?

换句话说,当您执行命令startService(intent)时,您是将意图放在单个队列中还是每次都启动一个新队列?

Intent someIntent1 = new Intent(this, myIntentService.class);
Intent someIntent2 = new Intent(this, myIntentService.class);
Intent someIntent3 = new Intent(this, myIntentService.class);
startService(someIntent1);
startService(someIntent2);
startService(someIntent3);

1 个答案:

答案 0 :(得分:12)

1)是否可以同时拥有多个intentService线程?

不,每个IntentService只有一个HandlerThread,用于按照调用“startService”的顺序执行请求。除非由于某种原因你决定在IntentService中产生自己的线程/线程,否则这可能会破坏首先使用IntentService的目的。同一清单声明的服务,即服务名称=“。MyIntentService”(这对于普通服务来说是相同的)在其进程中作为单例运行,因此在服务被终止之前,相同的服务将接收额外的启动请求。

2)如何在同一个IntentService上创建三个不同意图之间区分代码?

要区分请求,请按照预期使用Intent系统!为服务可以执行的不同作业提供不同的“操作”,并传递IntentService为该特定作业正确运行的任何额外内容,作为您用于启动服务的Intent对象中的附加内容。

相关问题