loop in IntentService

怎甘沉沦 提交于 2019-12-05 04:44:25

An IntentService is meant to finish of a task and return. It does this task in a new thread. Do not use while loop in IntentService. Your IntentService will get killed after sometime. I am telling this from personal experience. I tried using a while loop in it. And at the end of the while loop I used sleep(60000) i.e 1 minute. But I found that my IntentService was killed after sometime.

I would recommend you not to use an AlarmManager for 30 seconds, as some have siggested. Because 30 seconds is too short. it will drain the battery. For AlarmManager use a minimum 1 minute with RTC.

If you still want it to be 30 seconds, use a service. In the service use your logic. But do that in a separate thread i.e spawn a new thread in your Service and used while loop there and sleep(). And do not forget to use startForeGround. This reduces the probabilty of android killing your service greatly.

Using a while statement inside an IntentService, or any kind of Service for that matter is a bad idea. It is especially a bad idea inside an IntentService because the IntentService is supposed to finish a task and then get terminated automatically, you are in essence defeating the whole purpose of using an IntentService.

I would recommend to remove the loop in the IntentService and to use an alarm to wake up the IntentService every 30 seconds. That way, your service gets called every 30 seconds for sure and for the time that it is not processing, it can actually go back to sleep. Moreover, to handle cases where a new call to the IntentService is received while the IntentService is servicing an older request, you can add code to the onStartCommand method of your IntentService to see if the call should be enqueued for processing or ignored altogether.

Set an alarm using this method:

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

Link: http://goo.gl/E9e6

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

PS. We don't normally override the onStartCommand of an IntentService but it can be done if your app really that functionality.

Hossam Alaa

in this link you'll find a service that updates itself using a timer

Keep Service running

If your comfortable with the while loop just write an if statement that exists the loop

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

It would be better that you keep the loop or timer or any such running task in the MainActivity itself and execute IntentService everytime. Because IntentService will perform task and finish itself everytime or queue the task to be delivered further.

From the Docs -

IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

It uses work queue processor pattern to maintain the task.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!