Android WorkManager Constraints - Run Only if Network Available

val WORK_NAME = "backup"val constraints = Constraints.Builder()    .setRequiredNetworkType(NetworkType.CONNECTED)    .build()val work = OneTimeWorkRequestBuilder<UploadPhotoWorker>()    .setConstraints(constraints)    .build()val manager = WorkManager.getInstance(context)manager.enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, work)

If you don't want to run on Mobile Network (run on wired network only), use NetworkType.UNMETERED.

If you don't want to run while roadming, use NetworkType.NOT_ROAMING.

If the request if not time-sensitive, you might not want to run when roaming or when battery is slow.

val constraints = Constraints.Builder()    .setRequiredNetworkType(NetworkType.NOT_ROAMING)    .setRequiresBatteryNotLow(true)    .build()


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.