我正在尝试使用WorkManager进行服务器调用。因此,我编写了以下Worker类:
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;
}
}
我正在从我的Activity的onCreate()方法中调用这个工作类。代码段在这里:
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");
}
谢谢, Arindam.