计划计时器/优化TimerTask(Kotlin)

时间:2018-10-27 08:01:02

标签: timer timertask

请考虑以下内容:

var everySunday = Timer()
var timertask = object: TimerTask(){
    override fun run(){
         var day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
         var time = SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date()
         var midnight = "00:00:00"
         if (day == Calendar.Sunday && time == midnight){
               Toast.makeText(this, "It's Sunday!", Toast.LENGTH_SHORT).show()
            }
         }
}

everySunday.scheduleAtFixedRate(timertask, 0, 60000)

基本上,每个周日的功能是通知我,它在星期日的每个凌晨12:00都是星期天。

但是,我想进一步简化EverySunday的代码,仅此一点:

var everySunday = Timer()
var timertask = object: TimerTask(){
    override fun run(){
         Toast.makeText(this, "It's Sunday!", Toast.LENGTH_SHORT).show()
    }
}

//val date = ??  <-- "Sunday 12:00 am" 
everySunday.schedule(timertask, date)

问题是我想将日期写为日期!代表星期日12:00 am的类型,但我不确定该怎么做。协助将不胜感激。

0 个答案:

没有答案