Well after a bit more research I think I have concluded that my initial approach was way off. I am now changing it to use an AlarmManager instead. Can someone please explain AlarmManagers to me a bit better.
Previously this is how my app worked. Add an item to a database. Launch service. Service starts a Timer that runs a TimerTask every 5 minutes. TimerTask = For every row in the database If certain conditions are met, fire off a notification. If other conditions are met, delete that row Loop If there are now rows left, stop the service and timer. So how is the best way to now implement it with an AlarmManager. I have just set it up as follows. Add an item to a database. Start AlarmManager that runs every 5 minutes Broadcastreceiver = For every row in the database If certain conditions are met, fire off a notification. If other conditions are met, delete that row Loop If there are now rows left, cancel the AlarmManager This seems to work ok. Now I no longer need to run the service I previously was using at all. What I want to know is how long will the alarm keep running? Until it is explicitly canceled? Until the device is rebooted? What if my app is closed? Basically I just want to know if I am tackling this problem in the correct manner. Brad On Apr 27, 8:48 pm, "[email protected]" <[email protected]> wrote: > I have a service running that has a Timer. The Timer schedules a > TimerTask to run at a certain interval. The TimerTask simply display's > a notification. In theory it all sort of works but I find it very > inconsistent. > > The notifications only occur when the device is not sleeping. Also > sometimes it seems as though the Timer is only counting time when the > device is not asleep. If I leave the device asleep for 15 minutes then > wake it up after a minute or two I will get 4 notifications in a row. > All the ones that should have been displayed periodically over the > last 15 minutes. > > Am I handling this situation the correct way? Is a timer the correct > way to do this? How can I make the device wake from sleep and display > the notifications at the correct intervals? > > public class GameScoreService extends Service { > > Timer timer = null; > > private void startService() { > > timer = new Timer(); > > timer.scheduleAtFixedRate(new getNewScores(), 5000, 180000); > } > > class getNewScores extends TimerTask { > public void run() { > > ...perform notification > } > } > > @Override > public void onStart(Intent intent, int startId) { > > super.onStart(intent, startId); > > startService(); > } > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---

