简体   繁体   中英

How to make WorkManager alive when app is killed?

I am downloading file with "Coroutine Worker" work Manager, when application is in background the downloading work properly but when application is killed, work Manager stop working.

I tried to keep it alive with a boolean variable in an infinite loop to make it work but it didn't work as well.


class UploadWorker(
    private val appContext: Context,
    workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
    private var isDownloadCompleted = false


    override suspend fun doWork(): Result {
        val filName = inputData.getString("filName") ?: ""
        val url = inputData.getString("URL") ?: ""

        /*when file is downloaded, I change the status of the boolean to true, so it break the 
          loop*/

        //Loop break when download file is completed, occur error, or pause.
        while (!isDownloadCompleted) {
            Log.e("tag**", "downloaded file $isDownloadCompleted")
        }

        // Indicate whether the work finished successfully with the Result
        return Result.success()
    }

}

val imageData = workDataOf("URL" to url, "filName" to filName)
        val constraints = Constraints.Builder()
            .setRequiresBatteryNotLow(false)
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .setRequiresStorageNotLow(false)
            .build()
        Log.e("tag**", "createReq")

        val uploadWorkRequest: WorkRequest =
            OneTimeWorkRequestBuilder<UploadWorker>()
                .setInputData(imageData)
                .setConstraints(constraints)
                .setInitialDelay(0, TimeUnit.SECONDS)
                .setBackoffCriteria(
                    BackoffPolicy.LINEAR,
                    OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
                    TimeUnit.MILLISECONDS
                )
                .build()

I wanted to make it work whether application is killed or in background.

Check if this helps you ->

Kotlin ->


class UploadWorker(
        private val appContext: Context,
        workerParams: WorkerParameters
    ) : CoroutineWorker(appContext, workerParams) {
        private var isDownloadCompleted = false


        override suspend fun doWork(): Result {
            val filName = inputData.getString("filName") ?: ""
            val url = inputData.getString("URL") ?: ""

            /*when file is downloaded, I change the status of the boolean to true, so it break the
              loop*/

            //Loop break when download file is completed, occur error, or pause.
            while (!isDownloadCompleted) {
                Log.e("tag**", "downloaded file $isDownloadCompleted")
            }

            // Indicate whether the work finished successfully with the Result
            return Result.success()
        }

    }

Instead of using OneTimeWorkRequest, Try Using PeriodicWorkRequest, Hope it works.

val imageData = workDataOf("URL" to url, "filName" to filName)
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .setRequiresBatteryNotLow(false)
            .setRequiresStorageNotLow(false)
            .build()

        Log.e("tag**", "createReq")

        val uploadWorkRequest: WorkRequest =
            PeriodicWorkRequestBuilder<UploadWorker>(2, TimeUnit.HOURS)
                .setInputData(imageData)
                .setConstraints(constraints)
                .setInitialDelay(0, TimeUnit.SECONDS)
                .setBackoffCriteria(
                    BackoffPolicy.LINEAR,
                    OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
                    TimeUnit.MILLISECONDS
                )
                .build()

Nothing says that WorkManager will work if the app is killed. If the app is killed it is dead. That's it. What WorkManager ensures is that:

  • the application is less likely to be killed while a Work is in progress because WorkManager raised its own Service
  • If the application is killed and respectively a work is stopped - WM will ensure that the Work will be resumed in a later stage and it will finish for sure at some point when the Constraints are met.

All things are good but I forgot to attached a notification with Work Manager.

class UploadWorker(
    private val appContext: Context,
    workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
    private var isDownloadCompleted = false


    override suspend fun doWork(): Result {
        notification = NotificationCompat.Builder(
            applicationContext,
            appContext.getString(R.string.channel_name)
        )
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText("0MB/ $fileSize")
            .setOnlyAlertOnce(true)
            .setProgress(100, 0, false)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setOngoing(true)
            .setAutoCancel(false)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
        
        
        val filName = inputData.getString("filName") ?: ""
        val url = inputData.getString("URL") ?: ""
        //attach your notification, so workmanager will be killed
        setForegroundAsync(ForegroundInfo(id, notification.build()))

        /*when file is downloaded, I change the status of the boolean to true, so it break the 
          loop*/

        //Loop break when download file is completed, occur error, or pause.
        while (!isDownloadCompleted) {
            Log.e("tag**", "downloaded file $isDownloadCompleted")
        }

        // Indicate whether the work finished successfully with the Result
        return Result.success()
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM