Code Monkey home page Code Monkey logo

soworkmanager's Introduction

GitHub license

WorkManager

WorkManager is a library used to enqueue deferrable work that is guaranteed to execute sometime after its Constraints are met. WorkManager allows observation of work status and the ability to create complex chains of work. You can read about Constraints builder. WorkManager runs in the background you can get the status update using LiveData.

WorkManager Execution scenarios:

  1. When App in Foreground
  2. When App in the recent list
  3. If the app kills then WorkManager run pending tasks when app open again.

Do not forget that WorkManager work with constraints, when specified constraint met it will run tasks in the above scenarios.

We have developed a demo where we are downloading files & store it in sdCard. WorkManager help to download in the background as well as when Network constraint met. WorkManager have 2 options

- One time execution: It will run only one time
- Periodic execution: It will run periodically when constraint met for the task.

Here you can find steps to integrate WorkManager in your project.

STEP 1

Add dependency in application module Gradle file (build.gradle)

dependencies {
    implementation "androidx.work:work-runtime:2.0.0-rc01"
}

STEP 2

Create a work manager instance in onCreate() of activity

    val workManager = WorkManager.getInstance()

STEP 3

Create a work manager class extends with Worker

    class DownLoadFileWorkManager(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
        override fun doWork(): Result {
            //TODO perform your async operational task here
            /**
             * We have performed download task here on above example
             */
    
            return Result.success()
        }
    }

STEP 4

Set Constraints & start(enqueue) WorkManager task a) One Time task enqueue

    val constraints = androidx.work.Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
    val task = OneTimeWorkRequest.Builder(DownLoadFileWorkManager::class.java).setConstraints(constraints).build()
    workManager.enqueue(task)

b)Periodic task enqueue, Recommended periodic work interval minimum time is 900000 seconds or in other words 15 minutes

     val periodicWorkRequest = PeriodicWorkRequest.Builder(DownLoadFileWorkManager::class.java, PERIODIC_INTERVAL, TimeUnit.MINUTES)
            .setConstraints(Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
            .build()
        workManager.enqueue(periodicWorkRequest)

STEP 5

Get status of One time or periodic work Request task status.

    workManager.getWorkInfoByIdLiveData(task.id)
        .observe(this@MainActivity, Observer {
            it?.let {

                if (it.state == WorkInfo.State.RUNNING) {
                    //task running, you can update UI
                }else if (it.state.isFinished) {
                    // task finished you can notify to Views
                }
            }
        })

alt text

soworkmanager's People

Contributors

spaceoamit avatar

Stargazers

khanhsb22 avatar  avatar Wade avatar Himanshu Jain avatar Aome avatar  avatar Misho avatar  avatar 이승우 avatar Jquery Almeida avatar

Watchers

James Cloos avatar  avatar Jigar Mistry avatar Bhaval Patel avatar

Forkers

microcian vpsoft

soworkmanager's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.