Effect:
Packaged utility class: timeUtils.java
package com.xiao7.pump.Utils;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class TimeUtils {
/* Countdown time unit: second */
public static int COUNT = 20*60;
/* Currently do */
private static int CURR_COUNT = 0;
/* Expected end time */
private static long TIME_END = 0;
/* Timer */
private static Timer countdownTimer;
/* Displays the countdown textView*/
private static TextView txtCountdown;
/** * start countdown *@paramIsFirst Indicates whether it is the first time to enter *@paramSecond Indicates the countdown duration. The unit is second@paramTextView Displays the countdown textView */
public static void startCountdown(boolean isFirst,int second,TextView textView) {
COUNT = second;
long data = System.currentTimeMillis();
long time = TIME_END;
// The first time you enter, reassign
if(isFirst){
CURR_COUNT = COUNT;
time = data + COUNT * 1000;
TIME_END = time;
}else {
int the_difference = ((int) (time - data)) / 1000;
CURR_COUNT = the_difference;
}
// Start the countdown
txtCountdown = textView;
if (countdownTimer == null) {
countdownTimer = new Timer();
countdownTimer.schedule(new TimerTask() {
@Override
public void run(a) {
Message msg = newMessage(); msg.what = CURR_COUNT--; handler.sendMessage(msg); }},0.1000);
// The second parameter delay:"0" means: no delay
// The third parameter period:"1000" means how often (in milliseconds) is called.}}/** * end countdown */
public static void stopCountdown(a) {
// Send a message to end the countdown
Message message = new Message();
message.what = 0;
handler.sendMessage(message);
}
private static Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what <= 0) {
if(countdownTimer ! =null) {
countdownTimer.cancel();
countdownTimer = null;
}
txtCountdown.setText("Remaining: 00:00:00");
txtCountdown.setEnabled(true);
} else {
// Display the countdown effect
String txtTime = formatSecondTime(msg.what);
txtCountdown.setText(txtTime);
txtCountdown.setEnabled(false);
}
super.handleMessage(msg); }};/** * Time format method *@paramThe second number of seconds *@return* /
private static String formatSecondTime(int second) {
int hour = 0;
int minute = 0;
if (second > 60) {
minute = second / 60; / / integer
second = second % 60; / / to take over
}
if (minute > 60) {
hour = minute / 60;
minute = minute % 60;
}
String strtime = "Surplus:"+hour+"Hour"+minute+"Points"+second+"Seconds";
returnstrtime; }}Copy the code
TimeUtils call:
// Start the countdown button
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Suppose you count backwards for 20 minutes
int secondTime=20*60;
/ / the countdown
TimeUtils.startCountdown(true,secondTime,txtCountDown);
// The first argument is true, indicating the start of the countdown (first time)}});Copy the code
Activity lifecycle onRestart() is re-called as follows:
@Override
protected void onRestart(a) {
super.onRestart();
TimeUtils.startCountdown(false,secondTime,txtCountDown);
// The first argument is false, indicating that the countdown is not enabled for the first time and is running
}
Copy the code