TheJadav

Wrok constraints in android work manager

Wrok constraints in android work manager

In this post we will see different constraint which we can set when requesting work manager to do work.

NetworkType

Available options for network type is listed below, we can use any of constraint based on our requirement like  call api only when connected to network, or download large data when network unmetered.

  • CONNECTED : Connected to any network
  • METERED: 
  • NOT_REQUIRED
  • NOT_ROAMING
  • TEMPORARILY_UNMETERED
  • UNMETERED 

BatteryNotLow

when we set this parameter to true, work will not work if battery is low. 

RequiresCharging

This parameter checks device charging state and only execute work if it is in charging.

DeviceIdle

This ensures that device is not in use by user. This helps to ensure our resources are not used by other apps when we are executing work. 

StorageNotLow

This parameter checks storage availability in device and if it is set to true then it will only execute when device not running on low space.

here is simple demo to use constraint:

val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresCharging(true)
.build()

val myWorkRequest: WorkRequest =
OneTimeWorkRequestBuilder<MyWork>()
.setConstraints(constraints)
.build()

Thank you for reading this. 

Will post next update soon

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message