From 2d2ce3f95199f46e54c67fc376252bfb7815b0a3 Mon Sep 17 00:00:00 2001 From: Alberto Pasqualetto Date: Sat, 17 Jun 2023 19:32:57 +0200 Subject: [PATCH] 10 minutes timeout --- .../soundmeteresp/MeterService.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/src/main/java/it/albertopasqualetto/soundmeteresp/MeterService.kt b/app/src/main/java/it/albertopasqualetto/soundmeteresp/MeterService.kt index 70f6c81..80ed4fb 100644 --- a/app/src/main/java/it/albertopasqualetto/soundmeteresp/MeterService.kt +++ b/app/src/main/java/it/albertopasqualetto/soundmeteresp/MeterService.kt @@ -14,10 +14,13 @@ import android.media.MediaRecorder import android.os.IBinder import android.os.PowerManager import android.util.Log +import java.util.Timer +import java.util.TimerTask class MeterService : Service() { private lateinit var wakeLock: PowerManager.WakeLock + private lateinit var timer: Timer private lateinit var recordThread: AudioRecordThread private lateinit var readThread: AudioReadThread @@ -48,6 +51,7 @@ class MeterService : Service() { if (isRecording){ Log.d(TAG, "Service already running") wakeLock.acquire() // re-acquiring wakelock without timeout since now it is on screen + if (this::timer.isInitialized) timer.cancel() return START_NOT_STICKY } @@ -80,6 +84,16 @@ class MeterService : Service() { acquire() // starting service at startup or with play button } } + if (intent.getBooleanExtra(MAIN_ACTIVITY_PAUSE, false)) { + timer = Timer(true) + val timerTask: TimerTask = object : TimerTask() { + override fun run() { + Log.d(TAG, "TimerTask: stopSelf") + stopSelf() + } + } + timer.schedule(timerTask, 0, 600000) // stop service after 10 minutes of timeout if MainActivity has been paused + } Log.d(TAG, "Start recording thread") @@ -107,6 +121,7 @@ class MeterService : Service() { stopForeground(STOP_FOREGROUND_REMOVE) + if (this::timer.isInitialized) timer.cancel() wakeLock.release() super.onDestroy()