I have a service which gets called when the app is getting removed/closed. The service will start a OneTimeWorkRequest. Somehow the Workmanager will never start when onTaskRemoved is called.
Is there a way to make sure that when the app is killed the Workmanager gets to work?
This is my code: AndroidManifest.xml
<service android:name=".OnClearFromRecentService"
android:enabled="true"
android:exported="true"
android:stopWithTask="false" />
OnClearFromRecentService.kt
class OnClearFromRecentService : Service() {
override fun onBind(p0: Intent?): IBinder? {
TODO("Not yet implemented")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("log", "Service Started")
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
Log.d("log", "Service Destroyed")
}
override fun onTaskRemoved(rootIntent: Intent?) {
Log.d("log", "END")
val request = OneTimeWorkRequestBuilder<BackgroundTask>()
.addTag("oneTime")
.build()
WorkManager.getInstance(this).enqueue(request)
stopSelf()
}
}