The Android arsenal – background processing

A Kotlin Work Manager library for Android that includes notifications and Hilt support.

User guide

  1. Add the JitPack repository to your project’s build.gradle
allprojects { repositories { ... maven { url '' } }
  1. Add the plugins kapt and hilt to the build.gradle file of your app at the top
apply plugin: 'kotlin-kapt'
apply plugin: ''
  1. Add the dependencies for Boot Laces & Hilt to your app’s build.gradle
dependencies { implementation 'com.github.evilthreads669966:bootlaces:8.4' implementation "" kapt ""
  1. Add the Hilt plugin to the build.gradle dependencies of your project
dependencies { ... classpath "$hilt_version"
  1. Comment out your subclass of the application class
@HiltAndroidAppclass App: Application()
  1. Add the name of your application subclass to the manifest
<application android:name=".App" ...
  1. Create your employees. The description parameter is used for your notification. You can create a broadcast receiver for your worker by overriding onReceive an action to worker.
class MyWorker: Worker(66,"Something evil") { override val receiver: WorkReceiver? get() = null override suspend fun doWork(ctx: Context) { for(i in 1..10) delay(1000) }
}//if you want to add a BroadcastReceiver to your workerclass WorkerWithReceiver: Worker(666,"Locking the screen"){ override val receiver: WorkReceiver? get() = object : WorkReceiver(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) { override fun onReceive(ctx: Context?, intent: Intent?) { //do something } } override suspend fun doWork(ctx: Context) { //do work }
}//worker with a progress notificationclass MyProgressWorker: Worker(66,"Working while displaying a notification for progress", withNotification = true) { override val receiver: WorkReceiver? get() = null override suspend fun doWork(ctx: Context) { for(i in 1..10) delay(1000) }
  1. Inject your WorkScheduler into an Android context
@Inject lateinit var scheduler: WorkScheduler
  1. Schedule your employees
//persistent worker
scheduler.schedulePersistent(WorkerWithReceiver())//one time worker
scheduler.scheduleOneTime(MyWorker())//one time worker with notificaton for displaying progress. This works for all scheduler methods
scheduler.scheduleOneTime(MyProgressWorker())//periodic worker
scheduler.schedulePeriodic(10000, MyWorker()) //runs task every 10 seconds and persists through reboot//future worker
scheduler.scheduleFuture(5000, MyWorker()) //runs task once in 5 seconds and persists through reboot if device is restarted before//hourly worker
scheduler.scheduleHourly(MyWorker()) //runs task once every hour and persists through reboot//daily worker
scheduler.scheduleDaily(MyWorker()) //runs task once every day and persists through reboot//weekly worker
scheduler.scheduleWeekly(MyWorker()) //runs task once every week (7 days) and persists through reboot//monthly worker
scheduler.scheduleMonthly(MyWorker()) //runs task once every month and persists through reboot//yearly worker
scheduler.scheduleYearly(MyWorker()) //runs task once every year and persists through reboot

Important to know


Copyright 2019 Chris Basinger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox