Skip to content

Should we cancel sync to avoid retries when the device is largely offline? #2021

Answered by ndegwamartin
ellykits asked this question in Q&A
Discussion options

You must be logged in to vote

Workmanager provides a Constraints class object which we could use to specify that certain jobs should only run if there's an internet connection

For example:

Constraints constraints = new Constraints.Builder()
    .setRequiredNetworkType(NetworkType.CONNECTED)
    .build();

PeriodicWorkRequest request =
    new PeriodicWorkRequest.Builder(MyWorker.class, 15, TimeUnit.MINUTES)
        // Sets the input data for the ListenableWorker
        .setConstraints(constraints)
        .build();

We could also use NetworkType.NOT_REQUIRED to signal that that specific job Does Not require connectivity

For more see the documentation here: Android Background Work

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@ellykits
Comment options

ellykits Feb 14, 2023
Maintainer Author

Answer selected by ellykits
Comment options

You must be logged in to vote
1 reply
@ellykits
Comment options

ellykits Feb 14, 2023
Maintainer Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants