I am trying to make server call using WorkManager. So, I wrote the below Worker class:
class ServerCallWorker extends Worker {
public ServerCallWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.d("Testing", "ServerCallWorker constructor method" );
}
@NonNull
@Override
public Worker.Result doWork() {
Log.d("Testing", "doWork() method" );
return Worker.Result.SUCCESS;
}
}
This worker class I am calling from my onCreate() method of my activity. The code snippet is here:
private void getLocationUpdate() {
Log.d("Testing", "inside Location Update");
WorkManager workManager = WorkManager.getInstance();
Log.d("Testing", "inside Location Update 222");
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType
.CONNECTED).build();
// PeriodicWorkRequest recurringWork = new PeriodicWorkRequest.Builder(FilterWorker.class, 15,
// TimeUnit.MINUTES).build();
OneTimeWorkRequest wrkReq = new OneTimeWorkRequest.Builder(ServerCallWorker.class).setConstraints(constraints)
.build();
Log.d("Testing", "inside Location Update 333");
workManager.enqueue(wrkReq);
Log.d("Testing", "inside Location Update 444");
}
Now, getLocationUpdate() is getting called and it is printing the logs. But doWork() method is not getting called as no logs are showing. Same for ServerCallWorker() method.
Can someone please point out, where I am making wrong.
Thanks, Arindam.