Loop in IntentService

I have an infinite loop in my IntentService to update my view every 30 seconds based on the input of the main action.

public class IntentServiceTest extends IntentService { String Tag = "IntentServiceTest"; String ACTION_RCV_MESSAGE = "com.jsouptest8.intent.action.MESSAGE"; public IntentServiceTest(){ super("IntentServiceTest"); Log.d(Tag, "IntentServiceTest constructor"); } @Override protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub Log.d(Tag, "in onHandleIntent"); String url = intent.getStringExtra("URL"); Document doc; int i=0; try{ while(true){ Log.d(Tag, "entered try block..."); Log.d(Tag, "url = "+url); doc = Jsoup.connect(url) .get(); Log.d(Tag, "past Jsoup.connect"); Element data = doc.select("table").get(1).attr("bgcolor", "#f4f36f"); Log.d(Tag, data.toString()); Log.d(Tag, data.text()); Log.d(Tag, "creating intent..."); Intent broadcastIntent = new Intent(); Log.d(Tag, "setting action..."); broadcastIntent.setAction(ACTION_RCV_MESSAGE); broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); broadcastIntent.putExtra("OUTPUT", data.toString()); Log.d(Tag, "sending broadcast: "+(i++)); sendBroadcast(broadcastIntent); Thread.sleep(30*1000); } } catch(StackOverflowError e){ Log.d(Tag, "in StackOverflowError block..."); Log.d(Tag, "creating intent..."); Intent broadcastIntent = new Intent(); Log.d(Tag, "setting action..."); broadcastIntent.setAction(ACTION_RCV_MESSAGE); broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); broadcastIntent.putExtra("OUTPUT", "η³»η΅±εΏ™η·šδΈ­, θ«‹η¨εΎŒε†θ©¦"); Log.d(Tag, "sending broadcast..."); sendBroadcast(broadcastIntent); } catch(Exception e){ Log.d(Tag, "in catch Exception block..."); onHandleIntent(intent); } } } 

The problem is that I am stuck in this loop. Even if I kill the main action and then return to it to introduce a new input, the IntentService still returns based on the old input.

I need to know how I can update myself from the URL every 30 seconds without getting stuck. Thanks!

+7
source share
4 answers

An IntentService designed to complete the task and return. He performs this task in a new thread. Do not use the while loop in the IntentService. Your IntentService will be killed after some time. I say this from personal experience. I tried using a while loop. And at the end of the while loop, I used sleep(60000) ie 1 minute. But I found that my IntentService was killed after a while. I would recommend you not to use AlarmManager for 30 seconds, as some of them sighed. Because 30 seconds is too short. he will drain the battery. For AlarmManager, use a minimum of 1 minute with RTC .

If you still want it to be 30 seconds, use the service. Use your logic in the service. But do it in a separate thread, create a new thread in your service and use while loop and sleep() . And don't forget to use startForeGround . This reduces the likelihood that the android will greatly kill your service.

+6
source

Using a while statement inside an IntentService or any kind of Service in this regard is a bad idea. This is a particularly bad idea inside an IntentService, because an IntentService should complete the task and then automatically stop working, you essentially defeat the whole purpose of using IntentService.

I would recommend deleting the loop in the IntentService and use the alarm to wake up the IntentService every 30 seconds. Thus, your service is called every 30 seconds, and at the moment when it is not processed, it can really return to sleep. Moreover, to handle cases when a new IntentService call is received while the IntentService is serving an older request, you can add code to the onStartCommand method of your IntentService to find out if the call should be queued for processing or ignored altogether.

Set an alarm using this method:

public void setRepeating (type int, long triggerAtMillis, long intervalMillis, PendingIntent)

Link: http://goo.gl/E9e6

For a more efficient approach, use setInexactRepeating (but this does not guarantee a 30 second wakeup)

PS. We usually do not override onStartCommand IntentService, but this can be done if your application really has this functionality.

+2
source

in this link you will find a service that updates itself with a timer

Save service

If you're comfortable with the while loop, just write an if statement that contains the loop

 if(thisIsTrue) { break; // this will exit the loop! } 
+1
source

It would be better if you saved a loop or timer or any such running task in MainActivity itself and executed IntentService each time. Because the IntentService will complete the task and complete itself each time or queue task that will be delivered next.

From Documents -

IntentService will receive Intents, start the workflow, and stop servicing as needed.

It uses the work queue processor template to save the task.

0
source

All Articles