Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a repeating multiple interval timer in Android?

Tags:

android

I have searched through Google, SO and Android developers but I need some further help for decision making. This has been discussed in discrete subjects but I'd like to have a bit of help with insight/design-decisions.

What I'd like to accomplish is a kind of timer that plays an alarm after, say 45 minutes, and plays an alarm, say 15 minutes after that. When the last alarm has fired, the cycle restarts indefinitely. This would sequentially look like Start -> 45min countdown -> Alarm -> 15min countdown -> Alarm -> go back to 45min countdown to repeat cycle.

This is an attempt at constructing a Work/Break-cycle app but I'm having both specific problems as well as probable design-flaws.

My goal, in short:

  • Create a Work/Break-cycle that fires alarms at specific intervals
  • Make it survive phone-sleep (without necessarily forcing to have the screen on)
  • Update the UI to get a feedback of progress for each interval/timer

Currently I'm (in general, there are a few specifics that differ) using a Service that implements a Runnable with a delay matching the duration of the different timers. When the first delay is up (after 45 minutes), another Runnable is initialized and run (set to 15 minutes). This works pretty well. I make use of a partial wakelock for the Service and most of the times get this to run for about a cycle, but then things silently seem to stop. If I close the application and start it again, the cycle restarts.

Now, apart from this I'd like to update a UI with the amount of minutes left on each timer, which means I have to somehow, regularly, make "posts" to the UI in order to see a visual progress.

Am I going about this the right way? Should I have another approach on this and what would be a suitable one?

like image 705
shellström Avatar asked Nov 03 '25 11:11

shellström


1 Answers

Use the AlarmManager to schedule alarms, passing in whatever Intent you'd like to occur at the the time you've scheduled the alarm for. When 45 minute alarm goes off, schedule another alarm and when that one goes off, schedule another 45 minute alarm, and so on. EDIT

To update the UI each minute to tell how much time is left do something like this in your Activity:

public class MyActivity extends Activity {
int minutes = 45;
TextView tv;
public void onCreate(Bundle state) {
super.onCreate(state);
//set the content, grab your textview, etc...boilerplate
    Handler handler = new Handler();
    //schedule alarm here and post runnable as soon as scheduled
    handler.post(r)
}

    //somewhere else in your Class
     Runnable r = new Runnable() {
            @Override
            public void run() {
            if(!minutes == 0)
               tv.setText(minutes--);
               handler.postDelayed(this, 60000); //run the runnable in a minute again 
            else 
               handler.removeCallbacks(this);
            }
        };
}
like image 178
LuxuryMode Avatar answered Nov 05 '25 00:11

LuxuryMode