Question

I have an App where the user can create multiple countdown alarms. Each one has it's own name, time to count, ringtone, etc.

I extended the CountDownTimer to hold each alarm setup and created a Singleton manager to manage all the alarms.

Everything is working fine until I leave the App.

The counters don't actually die. They have a weird behavior. After starting a counter: 1) if I press back key until the home screen and then power off the screen the alarm will trigger as it should. 2) if I press the home button then turn off the screen it will trigger only if I open the app. 3) if I just turn off the screen while still on the app it will trigger as soon as I turn on the screen. I expected trouble because for that kind of thing I need to use Services, Handlers or other stuff like these.

The problem is, I'm new to Android and after reading a lot about it, I couldn't find a solution.

As far as I understood I should use Services, but it can have only one instance. How would I work with multiple alarms?

Is there other way of doing it?

I need a way to start each alarm and tell the system to keep counting no matter what happens! (and to call an Activity when it finishes)

Any help?

Was it helpful?

Solution

Is there other way of doing it?

If your goal is to alert the user of events in the future, regardless of whether your app is running, dump all your CountdownTimer logic, and switch to AlarmManager. It is specifically designed for this sort of scenario. You can set up an AlarmManager event for each timer, to get control when the end time is reached.

Also note that you will need to maintain information about your registered events in a file or database:

  • so users can add and remove them, and

  • so you can set up AlarmManager again if the user reboots their device

and to call an Activity when it finishes

Users may reject your app for that behavior. Not everyone wants to be interrupted in the middle of what they are doing with their device. Please allow this to be configurable, so the user can opt for something less intrusive, like a Notification.

OTHER TIPS

You should replace your CountdownTimers with AlarmManager (as in CommonsWare's answer), after setting it up it will trigger your function to execute on specified time, here is official guide:

http://developer.android.com/training/scheduling/alarms.html

Soon you will find that after device reboot your alarms are lost, you need to restart them by catching android.intent.action.BOOT_COMPLETED broadcast.

Also you should not do a lot of work in your alarm onReceive, you should pass information to some other component like IntentService, to make it safe while device is asleep you should use WakeLock (sample project).

[edit] ---- you should also store your alarm/ringtone related data in some persistant storage, you can use for it shared preferences, or (more work) sqlite database.

I you really want to use timer (i.e. when you want to show countdowns on screen): You don't have to have multiple timers to show multiple countdowns.

Everything you have to do is just put the target time into some collection, or event put it into the View.setTag().

Then you have timer (let say 1s tick) - on every single tick you have to iterate over all your gauges, check the target time and current time, parse the number and adjust the gauge.

However - as the timers works on separate (non main) threads - you will have to use handler or something like that. If you want to just set some alarms - take the CommonsWare's answer - it's good one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top