[embedyt] https://www.youtube.com/watch?v=lvibl8YJfGo[/embedyt]
activity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.codinginflow.countdowntimerexample.MainActivity"> <TextView android:id="@+id/text_view_countdown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="00:00" android:textColor="@android:color/black" android:textSize="60sp" /> <Button android:id="@+id/button_start_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text_view_countdown" android:layout_centerHorizontal="true" android:text="start" /> <Button android:id="@+id/button_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text_view_countdown" android:layout_marginStart="11dp" android:layout_toEndOf="@+id/button_start_pause" android:text="reset" android:visibility="invisible" tools:visibility="visible" /> </RelativeLayout>
package com.codinginflow.countdowntimerexample; import android.content.SharedPreferences; import android.os.CountDownTimer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final long START_TIME_IN_MILLIS = 600000; private TextView mTextViewCountDown; private Button mButtonStartPause; private Button mButtonReset; private CountDownTimer mCountDownTimer; private boolean mTimerRunning; private long mTimeLeftInMillis; private long mEndTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextViewCountDown = findViewById(R.id.text_view_countdown); mButtonStartPause = findViewById(R.id.button_start_pause); mButtonReset = findViewById(R.id.button_reset); mButtonStartPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mTimerRunning) { pauseTimer(); } else { startTimer(); } } }); mButtonReset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resetTimer(); } }); } private void startTimer() { mEndTime = System.currentTimeMillis() + mTimeLeftInMillis; mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) { @Override public void onTick(long millisUntilFinished) { mTimeLeftInMillis = millisUntilFinished; updateCountDownText(); } @Override public void onFinish() { mTimerRunning = false; updateButtons(); } }.start(); mTimerRunning = true; updateButtons(); } private void pauseTimer() { mCountDownTimer.cancel(); mTimerRunning = false; updateButtons(); } private void resetTimer() { mTimeLeftInMillis = START_TIME_IN_MILLIS; updateCountDownText(); updateButtons(); } private void updateCountDownText() { int minutes = (int) (mTimeLeftInMillis / 1000) / 60; int seconds = (int) (mTimeLeftInMillis / 1000) % 60; String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds); mTextViewCountDown.setText(timeLeftFormatted); } private void updateButtons() { if (mTimerRunning) { mButtonReset.setVisibility(View.INVISIBLE); mButtonStartPause.setText("Pause"); } else { mButtonStartPause.setText("Start"); if (mTimeLeftInMillis < 1000) { mButtonStartPause.setVisibility(View.INVISIBLE); } else { mButtonStartPause.setVisibility(View.VISIBLE); } if (mTimeLeftInMillis < START_TIME_IN_MILLIS) { mButtonReset.setVisibility(View.VISIBLE); } else { mButtonReset.setVisibility(View.INVISIBLE); } } } @Override protected void onStop() { super.onStop(); SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putLong("millisLeft", mTimeLeftInMillis); editor.putBoolean("timerRunning", mTimerRunning); editor.putLong("endTime", mEndTime); editor.apply(); if (mCountDownTimer != null) { mCountDownTimer.cancel(); } } @Override protected void onStart() { super.onStart(); SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE); mTimeLeftInMillis = prefs.getLong("millisLeft", START_TIME_IN_MILLIS); mTimerRunning = prefs.getBoolean("timerRunning", false); updateCountDownText(); updateButtons(); if (mTimerRunning) { mEndTime = prefs.getLong("endTime", 0); mTimeLeftInMillis = mEndTime - System.currentTimeMillis(); if (mTimeLeftInMillis < 0) { mTimeLeftInMillis = 0; mTimerRunning = false; updateCountDownText(); updateButtons(); } else { startTimer(); } } } }