0

I'm trying to schedule on Android a specific part of my code to be run every week (or every number of days, but set manually...). The thing is: I've been looking to the AlarmManager class, but I don't know if maybe the problem is that I don't know how to use it or what, but I don't know how to schedule part of my code to be launched every week.

Any help please?

Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • 1
    See here for example of doing something similar http://stackoverflow.com/questions/7845660/how-to-run-a-service-every-day-at-noon-and-on-every-boot/7846622#7846622. – Warpzit Oct 29 '11 at 19:50
  • 1
    I've tried to do something like that. I could set the alarm on, but it didn't repeat when expected, so what I'm asking for to use this method is for a more specific explanation. But thanks, it's very useful. – Sonhja Oct 30 '11 at 09:29

1 Answers1

1

I'd have a running thread that sleeps for the time that is set manually, remember that this time should be passed as milliseconds.

To model the time, I'd use the Calendar class, because is easier to user the constants it has, like Calendar.DAY_OF_WEEK. Yo can look on how to use this constants for the purpose you have.

Alvin Baena
  • 903
  • 1
  • 10
  • 24
  • If you have a thread as you say, wouldn't it be a waste of memory space on the Android device?? – Sonhja Oct 30 '11 at 09:30
  • Well, that's a good point, but it's a very little thread, and you can kill it every time it executes and create it again so the Garbage Collector can free some memory. You said you can use the AlarmManager class, I took a look at it and I think it's better than the thread, the methods listed in the class seem exactly what you're asking. – Alvin Baena Oct 30 '11 at 12:37
  • Good. I tried it and it works perfectly. As you say, I stop() the Thread once I started it, but obviously it finishes the thread. What I also did is combine it with a runnable object and, with the combination of both of them, I had my solution. Thanks a lot ^^ – Sonhja Oct 30 '11 at 18:14