https://github.com/mobiledevpro/android-workmanager-demo
Demo app using WorkManager API. Support Android 6 - 11. Official doc https://developer.android.com/topic/libraries/architecture/workmanager
https://github.com/mobiledevpro/android-workmanager-demo
android background-jobs clean-architecture kotlin material-ui material3 mvvm mvvm-architecture rxandroid2 rxjava2 workers workmanager workmanager-architecturecomponent workmanager-example workmanager-kotlin
Last synced: 5 months ago
JSON representation
Demo app using WorkManager API. Support Android 6 - 11. Official doc https://developer.android.com/topic/libraries/architecture/workmanager
- Host: GitHub
- URL: https://github.com/mobiledevpro/android-workmanager-demo
- Owner: mobiledevpro
- License: apache-2.0
- Created: 2021-12-03T10:45:47.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-20T15:47:56.000Z (over 1 year ago)
- Last Synced: 2024-11-07T18:51:04.214Z (7 months ago)
- Topics: android, background-jobs, clean-architecture, kotlin, material-ui, material3, mvvm, mvvm-architecture, rxandroid2, rxjava2, workers, workmanager, workmanager-architecturecomponent, workmanager-example, workmanager-kotlin
- Language: Kotlin
- Homepage: https://mobile-dev.pro
- Size: 843 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Android | Jetpack WorkManager | Demo
[](http://kotlinlang.org/)
[](https://lv.binarybabel.org/catalog/gradle/latest)
[](https://android-arsenal.com/api?level=23)
[](http://www.apache.org/licenses/LICENSE-2.0)[](https://www.codefactor.io/repository/github/mobiledevpro/android-workmanager-demo)
[](https://sonarcloud.io/summary/new_code?id=mobiledevpro_Android-WorkManager-Demo)
##
_WorkManager is the recommended solution for persistent work. Work is persistent when it remains scheduled through app restarts and system reboots. Because most background processing is best accomplished through persistent work, WorkManager is the primary recommended API for background processing._ [Read more in official docs](https://developer.android.com/topic/libraries/architecture/workmanager)
##
## 3 Steps to run periodic tasks in the background even the app is closed:
### #1 Init Jetpack WorkManager
```kotlin
WorkManager.getInstance(applicationContext)
```### #2 Setup Worker
```kotlin
class PriceAlerterWorker(
appContext: Context,
params: WorkerParameters,
private val interactor: PriceAlerterInteractor
) : RxWorker(appContext, params) {override fun createWork(): Single =
interactor
.createDemoAlert()
.map { result ->
when (result) {
is RxResult.Success -> {
//Do something on success
Result.success()
}
is RxResult.Failure -> {
//Do something on fail
Result.retry()
}
}
}
}```
### #3 Build request and run work
```kotlin
PeriodicWorkRequestBuilder(15, TimeUnit.MINUTES)
.setConstraints(
Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build()
)
.addTag(WORKER_PRICE_ALERT_TAG)
.build()
.let { request ->
workManager.runUniqueWork(
request,
"${WORKER_PRICE_ALERT_TAG}_periodic"
)
}
```## More about WorkManager:
### [WorkManager basics (article)](https://medium.com/androiddevelopers/workmanager-basics-beba51e94048)
### [WorkManager custom configuration and WorkerFactory (article)](https://medium.com/androiddevelopers/customizing-workmanager-fundamentals-fdaa17c46dd2)
### [Koin 3 + WorkManager (article)](https://medium.com/koin-developers/whats-next-with-koin-2-2-3-0-releases-6c5464ae5e3d)
## Notes:
+ The minimal interval for periodic tasks is 15 minutes, even if you set 1 min.
+ Retry with Backoff policy supports the minimum 10 sec and the maximum 5 hours interval (30 sec by
default).## Follow:
**Dmitri Chernysh**
[](https://www.youtube.com/@mobiledevpro?sub_confirmation=1)
[](https://www.instagram.com/mobiledevpro/)
[](https://twitter.com/mobiledev_pro)
[](https://www.linkedin.com/in/dmitriychernysh/)## License:
Copyright 2021 Dmitriy Chernysh
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 athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.