0

i have an IntentService that calls webservice in OnHandleIntent every 45 seconds using TimerTask.

my question is: i am calling on app start the IntentService, and in OnHandleIntent the task keeps repeating due to TimerTask..is it a good practice to do this or does this have any drawbacks? should i use an alarm manager in my activity to call the intent service every amount of time or its fine to keep on repeaing the task in OnHandleIntent using the timer task?

my code is like this:

 @Override
    protected void onHandleIntent(Intent intent)
    {

        context=this;   //INTENT CONTEXT

        final int timerValue = Integer.parseInt(MainActivitySharedPref.GetValue(context, "serviceTimer"));
        Log.d(TAG, "DOWNLOADSERVICE called having MainActivity.callService as: " + MainActivity.callService);
        t = new Timer();

        task = new TimerTask()
        {

            public void run() {
//run tasks
};
        t.scheduleAtFixedRate(task, 0, timerValue); // service executes task every 45 seconds

Thank you.

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58

1 Answers1

4

Is it a good practice to use TimerTask in OnHandleIntent in IntentService?

Absolutely not.

IntentService is designed to allow you to perform work in a supplied background thread via onHandleIntent(). It is not designed for you to fork threads, register listeners, set up TimerTask/ScheduledExecutorService, or do anything else that would be running past the end of onHandleIntent(). The IntentService will shut itself down once onHandleIntent() ends, after which Android may terminate your process within seconds, before your background threads (or, in this case, TimerTask) can do its work.

Please use a regular Service.

should i use an alarm manager in my activity to call the intent service every amount of time or its fine to keep on repeaing the task in OnHandleIntent using the timer task?

If you are doing this only while some activity of yours is in the foreground, the every-45-seconds part is OK. If you are trying to do this continuously, on battery-powered devices, be prepared to be attacked by users for the battery drain that you are causing.

But, while an activity of yours is in the foreground... ScheduledExecutorService (the modern replacement for TimerTask) in a regular Service should be fine. You should not need AlarmManager, which is specifically designed to give you control after your process has been terminated, for longer polling periods.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thank you for your response, so you recommend using a service and in the OnHandleIntent i perform the WebService calls same as in IntentService? – Rashad.Z May 13 '15 at 13:43
  • @user1950784: A regular `Service` does not have `onHandleIntent()`. In your `Service`, either in `onCreate()` or `onStartCommand()`, set up your `ScheduledExecutorService`, and the jobs invoked by `ScheduledExecutorService` will contain your code for talking to your Web service. – CommonsWare May 13 '15 at 13:44
  • but according to http://stackoverflow.com/questions/15524280/service-vs-intent-service "The Service runs in background but it runs on the Main Thread of the application" and i am calling in a webservice that returns sometimes thousands of queries to be executed.. so isnt an intent Service better "The IntentService runs on a separate worker thread." – Rashad.Z May 13 '15 at 14:10
  • i am kind of lost.. my app requires the user to sign in, after he signs in i trigger the intent service to execute periodically, and after the user signs out stopself() is called – Rashad.Z May 13 '15 at 14:11
  • @user1950784: `ScheduledExecutorService`, like `TimerTask`, invokes its jobs on background threads. The primary role of the `Service` is to indicate to Android that these background threads are doing work. – CommonsWare May 13 '15 at 14:36