Run IntentService several times.

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 I developed this well or not:

  • notification is a GCM notification that is extracted from db

My question is about the intent service, which I call several times. This is normal? How to improve it?

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 } 
+4
source share
1 answer

IntentService is intended for asynchronous use in a workflow. Therefore, there is nothing wrong with calling him several times, if necessary. Tasks will be executed one by one in the thread.

I assume that you have a class derived from IntentService and its onHandleIntent () is overridden. Only improvement I see that you do not need to create two separate intentions - this is basically the same intention with different additions in it. You can distinguish between the two in onHandleIntent ().

So your code should look like this:

 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 } 
+6
source

Source: https://habr.com/ru/post/1498002/


All Articles