首页 文章

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

提问于
浏览
11

我对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 回答

  • 11

    1) Is it possible to have multiple intentService threads at the same time or not?

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

    2) How do you differentiate in the code between creating three different intents on the same IntentService?

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

相关问题