首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多次启动IntentService

多次启动IntentService
EN

Stack Overflow用户
提问于 2013-08-20 13:24:15
回答 1查看 3.9K关注 0票数 6

我正在从事一个android项目,我正试图找到一种方法来改进下面的代码。我需要知道我开发的方法是否合适:

  • 通知是从db检索的GCM通知。

我的问题是关于我打了几次电话的意向服务。可以吗?应该如何改进呢?

代码语言:javascript
复制
while (((notification)) != null)
{{
    message = notification.getNotificationMessage();
    //tokenize message
    if ( /*message 1*/) {
         Intent intent1= new Intent(getApplicationContext(),A.class);
         intent1.putExtra("message1",true);
         startService(intent1);
    } else
    {
         Intent intent2= new Intent(getApplicationContext(),A.class);
         intent2.putExtra("message2",true);
         startService(intent2);
    }
}
//retrieve next notification and delete the current one
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-20 15:32:00

IntentService用于异步地在工作线程中运行。因此,如果需要的话,多次调用它是没有错的。意图将在一个线程中一个一个地运行。

我想您有一个派生自IntentService的类,它的onHandleIntent()被重写了。唯一的改进,我可以看到,你不需要创建两个不同的意图-这基本上是相同的意图,不同的附加在其中。您可以在onHandleIntent()中区分这两者。

因此,您的代码应该如下所示:

代码语言:javascript
复制
while (notification != null)
{
   Intent intent= new Intent(getApplicationContext(),A.class);
   message = notification.getNotificationMessage();

   if (msg1 condition) {
      intent.putExtra("message1",true);
   } else {
      intent.putExtra("message2",true);
   }

   startService(intent);

   //retrieve next notification and delete the current one
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18336244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档