I am working on an android project and I am trying to find a way to improve the following code . I need to know if the way I developed this is ok or not:
My question is about the intent service that I am calling several times. Is it ok? How should improve this?
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
}
IntentService is meant to be used in asynchronously to run in a worker thread. So nothing is wrong to call it multiple times if required. The intents will be running one by one in a thread.
I suppose you have a class derived from IntentService and its onHandleIntent() overridden. Only improvement I can see that you don't need to create two separate intents - it's basically same intent with different extras in it. You can differentiate these two in onHandleIntent().
So your code should look like:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With