How to initiate events based on stopwatch timer?

416 Views Asked by At

Basically I'm working on an application that alerts drivers how long they have been driving. It requires alerts at certain times.

At the moment I'm using a chronometer that displays time passed.

As one hour passes I need a message to appear on screen saying 'Hour Passed'.

As 2 hours passes I need another message saying 'Hour Passed' and a further message saying 'A 15 minute break is required'. Then a button needs to appear that allows the user to start a break which until starts a new chronometer.

When the break is complete at 15 minutes, a button needs to appear that says 'continue driving' and the original chronometer displaying driving time needs to continue from the time it was at before the break (2:00:01).

As you can see all the functionality is relatively similar, based on time events.

I'm guessing I need to use some sort of if statements that are initiated when the chronometer reaches a certain time but I'm basically stuck on how to make the application do the required functionality when the chronometer reaches a certain time.

2

There are 2 best solutions below

0
On

Try taking a look at the AlarmManager class to run an intent a certain amount of time after the time the stopwatch is started.

4
On

Try using the alarm manager with a pending intent.Create a receiver that catches the pending intent action and do whatever you want to do. You can either register a broadcastreceiver from your app when it is started(preferred way in your case) or create a broadcastrecievr and create a intent filter in the manifest file...In this case the event would be catched even if your app is not running.