I am trying to make an app that has a timer, which I have managed to implement, and that has a snooze timer countdown start ten seconds after the alarm ends if the stop button hasn't been pressed. I have been trying to figure this out for days now, and I am clearly doing something wrong but I am still very new to this and do not have the expertise yet to fix the issue. The issue occurs in the onFinish() method of my first CountDownTimer. After the the alarm rings, the screen will show "Time to get out!" as specified in the "if" statement, but my code appears to never proceed to the "else if" portion of my code. I can't tell if this is because I wrote the if/else if/else portion of my code incorrectly, or because I messed up making the timer to figure out how much time had passed. Any help is greatly appreciated! Thank you!!!!
public class CountDown extends ActionBarActivity {
private static long shower;
private static long snooze;
private Button stopButton;
private final long interval = 1 * 1000;
public TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_down);
Bundle extras = getIntent().getExtras();
shower = extras.getInt("EXTRA_SHOWER");
snooze = extras.getInt("EXTRA_SNOOZE");
final long showerMilli = shower * 60 * 1000;
final long snoozeMilli = snooze * 60 * 1000;
text = (TextView) findViewById(R.id.timeRemaining);
new CountDownTimer(showerMilli, interval) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text.setText("Time's up!");
MediaPlayer alarm = MediaPlayer.create(CountDown.this, R.raw.alarmsound);
alarm.start();
long timeElapsed = SystemClock.currentThreadTimeMillis();
long alarmTime = timeElapsed-showerMilli;
if (alarmTime <= 10000) {
text.setText("Time to get out!");
timeElapsed=SystemClock.currentThreadTimeMillis();
alarmTime = timeElapsed-showerMilli;
}
else if (alarmTime > 10000) {
alarm.stop();
new CountDownTimer(snoozeMilli, interval) {
public void onTick(long millisUntilFinished) {
text.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text.setText("Snooze is done!");
}
}.start();
}
else {
return;
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_count_down, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}