Since I need to perform work asynchronously in WorkManager, I need to use the ListenableWorker
, which by default runs on the main (UI) thread. Since this work c
Well - As the other answers mentioned, if you want to do frequent work (like every 60 sec) you should use foreground service.
Regardless, I'd use coroutines to get out of the main thread. Like:
runBlocking(Dispatchers.Default) {
//If you want to run blocking your code
}
Or using the GlobalScope Launch
GlobalScope.launch {
//Work in a separated thread (running not blocking)
}
Here is a practical example of getting a location using ListenableWorker and setting up the listeners out of the main thread.
https://github.com/febaisi/ListenableWorkerExample/blob/master/app/src/main/java/com/febaisi/listenableworkerexample/data/LocationListenableWorker.kt#L28