2022-07-05 03:33:42 +00:00
|
|
|
package com.massive
|
|
|
|
|
|
|
|
import android.app.Service
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.CountDownTimer
|
|
|
|
import android.os.IBinder
|
|
|
|
import android.util.Log
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
|
|
|
|
class TimerService : Service() {
|
2022-07-06 04:27:36 +00:00
|
|
|
private var countdownTimer: CountDownTimer? = null
|
2022-07-05 03:33:42 +00:00
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.M)
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
|
Log.d("TimerService", "Started timer service.")
|
|
|
|
val endMs = intent!!.extras!!.getInt("milliseconds")
|
2022-07-06 04:27:36 +00:00
|
|
|
countdownTimer?.cancel()
|
2022-07-05 03:33:42 +00:00
|
|
|
countdownTimer = object : CountDownTimer(endMs.toLong(), 1000) {
|
|
|
|
override fun onTick(currentMs: Long) {
|
2022-07-06 04:27:36 +00:00
|
|
|
val broadcastIntent = Intent(applicationContext, TimerBroadcast::class.java)
|
|
|
|
broadcastIntent.putExtra("endMs", endMs)
|
|
|
|
broadcastIntent.putExtra("currentMs", currentMs)
|
|
|
|
broadcastIntent.action = "tick"
|
|
|
|
sendBroadcast(broadcastIntent)
|
2022-07-05 03:33:42 +00:00
|
|
|
}
|
|
|
|
override fun onFinish() {
|
2022-07-06 04:27:36 +00:00
|
|
|
val broadcastIntent = Intent(applicationContext, TimerBroadcast::class.java)
|
|
|
|
broadcastIntent.action = "finish"
|
|
|
|
sendBroadcast(broadcastIntent)
|
2022-07-05 03:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 04:27:36 +00:00
|
|
|
countdownTimer!!.start()
|
2022-07-05 03:33:42 +00:00
|
|
|
return super.onStartCommand(intent, flags, startId)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
2022-07-06 04:27:36 +00:00
|
|
|
Log.d("TimerService", "Destroying...")
|
|
|
|
countdownTimer?.cancel()
|
|
|
|
val broadcastIntent = Intent(applicationContext, TimerBroadcast::class.java)
|
|
|
|
broadcastIntent.action = "stop"
|
|
|
|
sendBroadcast(broadcastIntent)
|
2022-07-05 03:33:42 +00:00
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
}
|