我正在从事一个android项目,我正试图找到一种方法来改进下面的代码。我需要知道我开发的方法是否合适:
我的问题是关于我打了几次电话的意向服务。可以吗?应该如何改进呢?
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
}发布于 2013-08-20 15:32:00
IntentService用于异步地在工作线程中运行。因此,如果需要的话,多次调用它是没有错的。意图将在一个线程中一个一个地运行。
我想您有一个派生自IntentService的类,它的onHandleIntent()被重写了。唯一的改进,我可以看到,你不需要创建两个不同的意图-这基本上是相同的意图,不同的附加在其中。您可以在onHandleIntent()中区分这两者。
因此,您的代码应该如下所示:
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
}https://stackoverflow.com/questions/18336244
复制相似问题