Я пытаюсь сделать так, чтобы служба выполнялась и выполнялась через определенные промежутки времени, с помощью этой статьи Мне удалось настроить службу и установить интервал в 1000 миллисекунд для входа в мою консоль, но я заметил, что служба запускается только один раз. Вот фрагмент моего кода:
class MessageService : Service() {
private var serviceLooper: Looper? = null
private var serviceHandler: ServiceHandler? = null
override fun onCreate() {
val context:Context = this
HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
serviceLooper = looper
serviceHandler = ServiceHandler(context, looper)
}
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show()
serviceHandler?.obtainMessage()?.also { msg ->
msg.arg1 = startId
serviceHandler?.sendMessage(msg)
}
return START_STICKY
}
override fun onBind(intent: Intent): IBinder? { return null}
override fun onDestroy() {}
private inner class ServiceHandler(context: Context, looper: Looper) : Handler(looper) {
val baseContext = context
override fun handleMessage(msg: Message) {
val runnable = Runnable {
Log.i("thread", "service has been called")
}
this.postDelayed(runnable, 1000)
}
}
}
пожалуйста, что я делаю не так?