Skip to content

Instantly share code, notes, and snippets.

@virendersran01
Forked from MrCrambo/work_manager.java
Created July 11, 2020 07:53
Show Gist options
  • Save virendersran01/802a119e189d077e516ffb9abe99af31 to your computer and use it in GitHub Desktop.
Save virendersran01/802a119e189d077e516ffb9abe99af31 to your computer and use it in GitHub Desktop.
public class UploadWorker extends Worker {
public UploadWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
}
@Override
public Result doWork() {
// Do the work here--in this case, upload the data to the server.
uploadData()
// Indicate whether the task finished successfully with the Result
return Result.success()
}
}
// define periodic work request and start it
PeriodicWorkRequest uploadWorkRequest =
new PeriodicWorkRequest.Builder(UploadWorker.class, 15, TimeUnit.MINUTES)
// setting a backoff on case the work needs to retry
.setBackoffCriteria(BackoffPolicy.LINEAR, PeriodicWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance(mContext).enqueue(uploadWorkRequest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment