|  |  |  | @ -0,0 +1,156 @@ | 
			
		
	
		
			
				
					|  |  |  |  | package net.micode.notes.timer; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import android.content.Intent; | 
			
		
	
		
			
				
					|  |  |  |  | import android.os.Bundle; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.LayoutInflater; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.View; | 
			
		
	
		
			
				
					|  |  |  |  | import android.widget.Button; | 
			
		
	
		
			
				
					|  |  |  |  | import android.widget.Toast; | 
			
		
	
		
			
				
					|  |  |  |  | import android.widget.TextView; | 
			
		
	
		
			
				
					|  |  |  |  | import android.os.CountDownTimer; | 
			
		
	
		
			
				
					|  |  |  |  | import android.media.MediaPlayer; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.Menu; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.MenuItem; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.MenuInflater; | 
			
		
	
		
			
				
					|  |  |  |  | import android.view.MenuItem.OnMenuItemClickListener; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import androidx.activity.EdgeToEdge; | 
			
		
	
		
			
				
					|  |  |  |  | import androidx.appcompat.app.AppCompatActivity; | 
			
		
	
		
			
				
					|  |  |  |  | import androidx.core.graphics.Insets; | 
			
		
	
		
			
				
					|  |  |  |  | import androidx.core.view.ViewCompat; | 
			
		
	
		
			
				
					|  |  |  |  | import androidx.core.view.WindowInsetsCompat; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import net.micode.notes.R; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | public class CountDown extends AppCompatActivity implements CountDownListener { | 
			
		
	
		
			
				
					|  |  |  |  |     private Button startButton,resetButton,stopButton; | 
			
		
	
		
			
				
					|  |  |  |  |     private static final int HOUR = 60*60*1000;//小时
 | 
			
		
	
		
			
				
					|  |  |  |  |     private static final int MIN = 60*1000;//分钟
 | 
			
		
	
		
			
				
					|  |  |  |  |     private static final int SEC = 1000;//秒
 | 
			
		
	
		
			
				
					|  |  |  |  |     private long hour,min,sec; | 
			
		
	
		
			
				
					|  |  |  |  |     private boolean IsRunning;//是不是在跑
 | 
			
		
	
		
			
				
					|  |  |  |  |     private long startTime;//开始时间
 | 
			
		
	
		
			
				
					|  |  |  |  |     private long currentTime;//还剩多久
 | 
			
		
	
		
			
				
					|  |  |  |  |     private CountDownTimer timer; | 
			
		
	
		
			
				
					|  |  |  |  |     private CountDownListener listener; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     @Override | 
			
		
	
		
			
				
					|  |  |  |  |     protected void onCreate(Bundle savedInstanceState) { | 
			
		
	
		
			
				
					|  |  |  |  |         super.onCreate(savedInstanceState); | 
			
		
	
		
			
				
					|  |  |  |  |         EdgeToEdge.enable(this); | 
			
		
	
		
			
				
					|  |  |  |  |         //getWindow().setBackgroundDrawableResource(R.drawable.cloud);
 | 
			
		
	
		
			
				
					|  |  |  |  |         setContentView(R.layout.activity_count_down); | 
			
		
	
		
			
				
					|  |  |  |  |         startButton = findViewById(R.id.button_start); | 
			
		
	
		
			
				
					|  |  |  |  |         resetButton = findViewById(R.id.button_reset); | 
			
		
	
		
			
				
					|  |  |  |  |         stopButton = findViewById(R.id.button_stop);//按钮
 | 
			
		
	
		
			
				
					|  |  |  |  |         startButton.setOnClickListener(new View.OnClickListener() { | 
			
		
	
		
			
				
					|  |  |  |  |             @Override public void onClick(View view) { | 
			
		
	
		
			
				
					|  |  |  |  |                 start(); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         resetButton.setOnClickListener(new View.OnClickListener() { | 
			
		
	
		
			
				
					|  |  |  |  |             @Override public void onClick(View view) { | 
			
		
	
		
			
				
					|  |  |  |  |                 reset(); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         stopButton.setOnClickListener(new View.OnClickListener() { | 
			
		
	
		
			
				
					|  |  |  |  |             @Override public void onClick(View view) { | 
			
		
	
		
			
				
					|  |  |  |  |                 stop(); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  |         Intent intent = this.getIntent(); | 
			
		
	
		
			
				
					|  |  |  |  |         hour=intent.getLongExtra("hour", 0L); | 
			
		
	
		
			
				
					|  |  |  |  |         min=intent.getLongExtra("min", 0L); | 
			
		
	
		
			
				
					|  |  |  |  |         sec=intent.getLongExtra("sec", 0L); | 
			
		
	
		
			
				
					|  |  |  |  |         currentTime=startTime=hour*HOUR+min*MIN+sec*SEC; | 
			
		
	
		
			
				
					|  |  |  |  |         updateTime(startTime); | 
			
		
	
		
			
				
					|  |  |  |  |         IsRunning=false; | 
			
		
	
		
			
				
					|  |  |  |  |         start();//开始跑
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { | 
			
		
	
		
			
				
					|  |  |  |  |             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); | 
			
		
	
		
			
				
					|  |  |  |  |             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); | 
			
		
	
		
			
				
					|  |  |  |  |             return insets; | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     public void setTime(long duration){ | 
			
		
	
		
			
				
					|  |  |  |  |         if(IsRunning)return; | 
			
		
	
		
			
				
					|  |  |  |  |         currentTime=startTime=duration; | 
			
		
	
		
			
				
					|  |  |  |  |         updateTime(duration); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public void start() { | 
			
		
	
		
			
				
					|  |  |  |  |         if(IsRunning) {return;}//还在跑
 | 
			
		
	
		
			
				
					|  |  |  |  |         IsRunning = true; | 
			
		
	
		
			
				
					|  |  |  |  |         timer = new CountDownTimer(currentTime, 100){//精度100ms
 | 
			
		
	
		
			
				
					|  |  |  |  |             @Override public void onTick(long millis){ | 
			
		
	
		
			
				
					|  |  |  |  |                 currentTime = millis; | 
			
		
	
		
			
				
					|  |  |  |  |                 updateTime(millis); | 
			
		
	
		
			
				
					|  |  |  |  |                 //invalidate();
 | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             @Override public void onFinish() { | 
			
		
	
		
			
				
					|  |  |  |  |                 stop(); | 
			
		
	
		
			
				
					|  |  |  |  |                 if (listener != null) { | 
			
		
	
		
			
				
					|  |  |  |  |                     listener.onFinishCountDown(); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         }; | 
			
		
	
		
			
				
					|  |  |  |  |         timer.start(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public void reset() { | 
			
		
	
		
			
				
					|  |  |  |  |         stop(); | 
			
		
	
		
			
				
					|  |  |  |  |         setTime(startTime); | 
			
		
	
		
			
				
					|  |  |  |  |         //invalidate();
 | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public void stop() { | 
			
		
	
		
			
				
					|  |  |  |  |         if(!IsRunning){return;}//已经停了
 | 
			
		
	
		
			
				
					|  |  |  |  |         IsRunning=false; | 
			
		
	
		
			
				
					|  |  |  |  |         timer.cancel(); | 
			
		
	
		
			
				
					|  |  |  |  |         setListener(this);//监听器
 | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     public void updateTime(long duration){//展示倒计时数字
 | 
			
		
	
		
			
				
					|  |  |  |  |         TextView timeView = findViewById(R.id.timeshow); | 
			
		
	
		
			
				
					|  |  |  |  |         String res=getTimeShow(duration); | 
			
		
	
		
			
				
					|  |  |  |  |         timeView.setText(res); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     public String getTimeShow(long duration){//获取显示数字
 | 
			
		
	
		
			
				
					|  |  |  |  |         int hour=(int)duration/HOUR; | 
			
		
	
		
			
				
					|  |  |  |  |         duration%=HOUR; | 
			
		
	
		
			
				
					|  |  |  |  |         int min=(int)duration/MIN; | 
			
		
	
		
			
				
					|  |  |  |  |         duration%=MIN; | 
			
		
	
		
			
				
					|  |  |  |  |         int sec=(int)duration/SEC; | 
			
		
	
		
			
				
					|  |  |  |  |         String format="%02d"; | 
			
		
	
		
			
				
					|  |  |  |  |         String Shour = String.format(format, hour); | 
			
		
	
		
			
				
					|  |  |  |  |         String Smin = String.format( format, min); | 
			
		
	
		
			
				
					|  |  |  |  |         String Ssec = String.format(format, sec); | 
			
		
	
		
			
				
					|  |  |  |  |         return String.format("%s h : %s m : %s s", Shour, Smin, Ssec); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     public void setListener(CountDownListener listener) { | 
			
		
	
		
			
				
					|  |  |  |  |         this.listener = listener; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  |     @Override | 
			
		
	
		
			
				
					|  |  |  |  |     public void onFinishCountDown() {//倒计时截至
 | 
			
		
	
		
			
				
					|  |  |  |  |         //Toast.makeText(this, "Time up !!!", Toast.LENGTH_SHORT).show();
 | 
			
		
	
		
			
				
					|  |  |  |  |         TextView timeView = findViewById(R.id.timeshow); | 
			
		
	
		
			
				
					|  |  |  |  |         timeView.setText("Time up !!!"); | 
			
		
	
		
			
				
					|  |  |  |  |         MediaPlayer player=MediaPlayer.create(this,R.raw.timedown); | 
			
		
	
		
			
				
					|  |  |  |  |         player.start(); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 |