Michel
Michel

Reputation: 23635

Which kind of object should i use for a once-per-5-minute check program?

I'm about to create a little Android program which performs a small task every say 5 minutes. Then the program can 'stop' and wait for anther 5 minutes and perform the same small task.

I call this program the 'service'

I should be able to communicate with the program (can be via a setting if that is possible, or talking directly to the service) via another application (or can it be the same?) which has a small UI to set some settings for the small activity.

Now I've read about these constructs, and I'm not sure which one to use:

  1. Service
  2. IntentService
  3. AlarmManager
  4. Handler

Can you shine a light on whic one to use?

Ps

I can imagine my description of the goal is a little hard to understand. Best thing to compare it to I think is the icon tray on a windows pc: it's a program/service which starts automaticly and does some things (in my case, it does the things periodically) and you can have some interaction with it when you click the icon. So my program must be running uin the background, but I must be able to communicate with the program.

EDIT Maybe a better example is this: there is a program/service that checks wether there are new WiFi networks available near your phone. That service checks periodically (i guess) for the presence of new networks. You can however turn off this behaviour via a setting (which is what i mean with the GUI interacting with the service)

Upvotes: 1

Views: 96

Answers (3)

tyczj
tyczj

Reputation: 73926

Your best bet is to use both the AlarmManager and IntentService. that way you start the alarm and every 5 min or whatever you send an intent to the intent service to do whatever you want to do.

The intent service will stop itself once all tasks are done so you dont have to worry about managing it

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93678

Here's the difference between these:

Service- runs permanently (or until stopped due to resources). Does not keep the phone awake. Is not scheduled, must see some event occur or be called in order to do work.

IntentService- runs long enough to do a task. Can queue up several tasks. Does not keep the phone awake. Is not scheduled, but does queue up requests.

AlarmManager- Must exist in the context of an Activity or Service. Will wake up the phone. Runs on a schedule.

Handler- runs when an event comes in. Does not keep the phone awake. Can be scheduled, but because it will not wake the phone should only be used for a short term delay.

You need an AlarmManager and a Service. The Service will run and allow your Alarm somewhere to live. The alarm itself will be called on a schedule and will wake up the phone to do its task.

Upvotes: 1

Khantahr
Khantahr

Reputation: 8548

Without knowing exactly what the tasks you're performing are, I'm going to say you need to use AlarmManager to manage the timing of the tasks, an IntentService to run the tasks themselves.

Seems like you just want the user to be able to set preferences on the service? You'd use an Activity for that, saving the settings using SharedPreferences, which your service could read from.

Upvotes: 2

Related Questions