Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Instances of IntentService in Android

    private void setFPAlarm()
    {
    Intent intent = new Intent(this, FPService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long nextSearchTimeMillis = DateUtils.MINUTE_IN_MILLIS/2;
    Time nextSearchTime = new Time();
    nextSearchTime.set(nextSearchTimeMillis);

    AlarmManager FPAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    FPAlarm.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), nextSearchTimeMillis, pi);
    }

I am using above code to run my IntentService every 30 seconds. Sometimes, a service process takes longer than 30 seconds so another one has to start before the previous one is finished. I want to know what happens to the previous one in that case. Is it put on hold? Does the second one wait for the previous one to finish?

My second question is: I do not want them to wait for each other. I want two services to run concurrently. So the next service should start regardless of what the previous one is doing. Is above code the right way to achieve this?

like image 872
Erol Avatar asked Jun 29 '12 17:06

Erol


1 Answers

IntentService is like a work request queue. A new intent isn't handled until onHandleIntent finishes for the previous Intent. Why do you want to run the IntentService every 30 seconds if it sometimes takes longer than 30 seconds to finish?

In regard to asking questions, sometimes the answer to a narrow question is either "you can't do what you want to do" or "what you're doing won't do what you think it does". If you provide the problem context, it's easier for respondents to add a better solution.

In your case, I can only guess that you should fire an Intent to your IntentService whenever you need to, and let IntentService's queuing mechanism handle the rest. As an alternative, you can use a regular service and a threadpool.

like image 55
Joe Malin Avatar answered Nov 08 '22 15:11

Joe Malin