TaskConfig¶
Configuration object passed to BackgroundFetch.scheduleTask.
delay¶
fun setDelay(milliseconds: Long): Builder
The minimum delay in milliseconds before this task fires.
The OS may delay the task beyond this value depending on device conditions, battery state, and other scheduling constraints. Example
// Fire at least 30 seconds from now.
val config = BackgroundFetchConfig.Builder()
.setTaskId("com.foo.task")
.setDelay(30000L)
.build()
BackgroundFetch.getInstance(context).scheduleTask(config)
periodic¶
fun setPeriodic(value: Boolean): Builder
Set true to schedule a repeating task. Defaults to false (one-shot).
When true, the task automatically re-schedules itself after each execution
using the same TaskConfig.delay as the repeat interval.
Example
val config = BackgroundFetchConfig.Builder()
.setTaskId("com.foo.periodic")
.setDelay(900000L) // 15 minutes in ms
.setPeriodic(true)
.build()
BackgroundFetch.getInstance(context).scheduleTask(config)
taskId¶
fun setTaskId(taskId: String): Builder
A unique identifier for this task.
The same taskId is delivered to the onEvent callback registered in
BackgroundFetch.configure. Pass it to BackgroundFetch.finish to signal
completion and to BackgroundFetch.stop to cancel the task.
Use reverse-domain notation to avoid collisions with other tasks. Example
val config = BackgroundFetchConfig.Builder()
.setTaskId("com.myapp.sync")
.setDelay(60000L)
.build()
BackgroundFetch.getInstance(context).scheduleTask(config)
// In your configure() callback:
override fun onFetch(taskId: String) {
if (taskId == "com.myapp.sync") {
performSync()
}
BackgroundFetch.getInstance(context).finish(taskId)
}
forceAlarmManager Android only¶
fun setForceAlarmManager(value: Boolean): Builder
Force the use of Android's AlarmManager API instead of
JobScheduler. Defaults to false.
By default the plugin uses JobScheduler on API 21+ and falls back to AlarmManager
on older devices. Set true to force AlarmManager regardless of API level.
Note
AlarmManager bypasses the scheduling optimisations of JobScheduler and
is less battery-efficient. Use it only when JobScheduler does not meet your timing
requirements.
requiredNetworkType Android only¶
fun setRequiredNetworkType(networkType: Int): Builder
Specify the kind of network connectivity required to run this task. Defaults to NetworkType.NETWORK_TYPE_NONE (no network required).
If the required network type is unavailable, the task will not run until the constraint is satisfied.
| Value | Constant | Description |
|---|---|---|
0 |
NetworkType.NETWORK_TYPE_NONE | No network constraint. |
1 |
NetworkType.NETWORK_TYPE_ANY | Any active network connection. |
2 |
NetworkType.NETWORK_TYPE_CELLULAR | Cellular (mobile data) connection. |
3 |
NetworkType.NETWORK_TYPE_UNMETERED | Unmetered (e.g. Wi-Fi) connection. |
4 |
NetworkType.NETWORK_TYPE_NOT_ROAMING | Non-roaming connection. |
On iOS, use TaskConfig.requiresNetworkConnectivity instead. Example
val config = BackgroundFetchConfig.Builder()
.setTaskId("com.foo.sync")
.setDelay(60000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.build()
requiresBatteryNotLow Android only¶
fun setRequiresBatteryNotLow(value: Boolean): Builder
Set true to require the device's battery level to be above the
"low battery" threshold before running this task. Defaults to false.
requiresCharging Android only¶
fun setRequiresCharging(value: Boolean): Builder
Set true to require the device to be charging (or connected to
permanent power) before running this task. Defaults to false.
requiresDeviceIdle Android only¶
fun setRequiresDeviceIdle(value: Boolean): Builder
Set true to require the device to be idle (not actively used)
before running this task. Defaults to false.
This is a good option for resource-intensive tasks. Battery usage is still attributed to your app and surfaced to the user in battery stats.
requiresStorageNotLow Android only¶
fun setRequiresStorageNotLow(value: Boolean): Builder
Set true to require the device's available storage to be above
the "low storage" threshold before running this task. Defaults to false.
startOnBoot Android only¶
fun setStartOnBoot(value: Boolean): Builder
Set true to resume background-fetch events when the device is
rebooted. Defaults to false.
Note
Requires stopOnTerminate false.
stopOnTerminate Android only¶
fun setStopOnTerminate(value: Boolean): Builder
Set false to continue receiving background-fetch events after the
user terminates the app. Defaults to true.
Note
startOnBoot requires stopOnTerminate: false.
Example
val config = BackgroundFetchConfig.Builder()
.setStopOnTerminate(false)
.setStartOnBoot(true)
.build()