Commit c91b0af2 authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

MainActivity: Fix piling up of background jobs.

parent 79ee49b7
......@@ -31,6 +31,7 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import de.ccc.events.badge.card10.background.SetClockJob
......@@ -41,6 +42,8 @@ import java.util.concurrent.TimeUnit
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 1
private const val REQUEST_CODE_PERMISSIONS = 2
private const val WORK_NAME = "update_clock"
class MainActivity : AppCompatActivity() {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
......@@ -104,13 +107,13 @@ class MainActivity : AppCompatActivity() {
val workManager = WorkManager.getInstance(this)
if (!bluetoothAdapter.bondedDevices.isEmpty()
) {
val setClockRequest =
val updateClockRequest =
PeriodicWorkRequestBuilder<SetClockJob>(1, TimeUnit.MINUTES)
.build()
workManager
.enqueue(setClockRequest)
.enqueueUniquePeriodicWork(WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, updateClockRequest)
} else {
// TODO cancel job
workManager.cancelUniqueWork(WORK_NAME)
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment