KickAss
KickAss

Reputation: 4290

Android Java Service and Timer/Thread Sleep

Basically, what I need to code is simple, but as I'm a beginner, I am still confused about Service, IntentService, creating a new Thread separate from the main Activity/UI thread, etc. I'll explain the Task first and then, how I think this might be best achieved.

Task

The Main Activity/UI has two buttons, Start and Stop. The App should make a cycle of two tasks, TaskA and TaskB. When Start is pressed, the cycle is executed. The cycle has these steps inside-

  1. Update Status on the Main Activity's UI: "TaskA execution in 60 seconds"
  2. Wait 60 seconds
  3. Update Status ... : "TaskA Executing"
  4. Execute TaskA
  5. Update Status ... : "TaskB execution in 120 second"
  6. Wait 120 seconds
  7. Update Status ... : "TaskB Executing"
  8. Execute TaskB

On completion, the cycle repeats. This cycle must continue until the user presses Stop. This cycle must not stop, be interrupted or be destroyed if the Main Activity/UI is onPause, onStop, or onDestroy. The Main Activity/UI should be completely separate from the cycle thread.

Stop button: Stops the execution of all the Steps inside the cycle, regardless of which step it is. Cancel all scheduled tasks and Stop the Service. The state the App should be at now must be as if the App is being started the first time (the same state it was before Start was pressed).

Problem

I am unsure the full functions of Service and IntentService. I know Service can perform multi-threading whereas IntentService performs a task queue and performs them one by one.

Potential Solution

My cycle above needs to perform those 8 steps in that exact order so IntentService would be the ideal solution.

But my question is: can I create an IntentService that is completely independent from the Main Activity/UI thread so that it does not rely on the Main Activity's lifecycle or thread?

If not, what would be the ideal alternative to achieve this?

Thanks

Upvotes: 1

Views: 1763

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007554

My cycle above needs to perform those 8 steps in that exact order so IntentService would be the ideal solution.

Not really. IntentService is designed for transactional sorts of work: do a few seconds, or maybe minutes, of work, then go away. Your need is for something that will run more indefinitely.

can I create an IntentService that is completely independent from the Main Activity/UI thread so that it does not rely on the Main Activity's lifecycle or thread?

An IntentService usually has very little to do with your process' main application thread.

A better solution would be a regular Service. Use a standard Java ScheduledExecutorService to handle the timing of the events and triggering their work on a background thread. Start that work in onStartCommand() of the service, triggered by a startService() call from your activity. Stop that work in onDestroy() of your service, triggered by a stopService() call from your activity. In between startService() and stopService(), your service will run... for a while, at least.

Even better, if your polling period were longer, would be to use AlarmManager and have it give you control at this event points in time. That way, your Service would not be clogging up memory all of the time. As it stands, users will be prone to task-kill your app, if they do not value your service.

Upvotes: 3

Related Questions