如何将参数传递给 WorkManager 的 DoWork 方法

huangapple go评论80阅读模式
英文:

How to pass parameters to Workmanager DoWork method

问题

我想要安排任务,在 24 小时后从数据库中删除。

public class WorkManager extends Worker {

    public WorkManager(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        // 在这里执行删除任务的操作
        // 从输入数据中获取要删除的任务
        Data inputData = getInputData();
        Task taskToDelete = inputData.getSerializable("taskToDelete");

        // 执行删除操作
        if (taskToDelete != null) {
            deleteTask(taskToDelete);
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    public void deleteTask(Task task) {
        DataBaseHelper db = new DataBaseHelper(getApplicationContext());
        db.deleteOne(task);
    }
}

如何将要删除的任务作为参数传递给 DoWork 方法,就像这样...

// 在调度任务时,将要删除的任务放入输入数据中
Data inputData = new Data.Builder()
    .putSerializable("taskToDelete", taskToDelete)
    .build();

OneTimeWorkRequest deleteTaskRequest =
    new OneTimeWorkRequest.Builder(WorkManager.class)
        .setInputData(inputData)
        .build();

WorkManager.getInstance(context).enqueue(deleteTaskRequest);
英文:

I want to Schedule task to be deleted from the database after 24 hours

public class WorkManager extends Worker {

    public WorkManager(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        return null;
    }}

how can i pass the Task to be deleted as a parameter to the DoWork method like so...

public void deleteTask(Task task){
        DataBaseHelper db = new DataBaseHelper(context);
        db.deleteOne(task);
    }

答案1

得分: 2

Worker类仍不支持将自定义对象作为参数传递给Data。您可以调整deleteOne方法,根据id删除任务,并将要删除的id传递给Worker

public static OneTimeWorkRequest create(String id) {
    Data inputData = new Data.Builder()
            .putString(TASK_ID, id)
            .build();
    return new OneTimeWorkRequest.Builder(SampleWorker.class)
            .setInputData(inputData)
            .setInitialDelay(24, TimeUnit.HOURS)
            .build();
}

...

@NonNull
@Override
public Result doWork() {
    String taskId = getInputData().getString(TASK_ID);

    // ...
    // ...
}

如果您仍然坚持要将Task作为参数传递给您的Worker,可以尝试以下代码:

public static OneTimeWorkRequest create(Task task) {
    String strTask = new Gson().toJson(task);
    Data inputData = new Data.Builder()
            .putString(TASK, strTask)
            .build();
    return new OneTimeWorkRequest.Builder(SampleWorker.class)
            .setInputData(inputData)
            .setInitialDelay(24, TimeUnit.HOURS)
            .build();
}

...

@NonNull
@Override
public Result doWork() {
    String strTask = getInputData().getString(TASK);
    Task task = new Gson().fromJson(strTask, Task.class);

    // ...
    // ...
}

build.gradle中添加以下依赖项以使用Gson

implementation 'com.google.code.gson:gson:2.8.6'

有关更多信息和示例,请查看此处

英文:

Worker class still does not support custom object as parameters to pass in Data. What you can do is tweak your deleteOne method to delete task based on id and pass this id to be deleted to Worker.

  public static OneTimeWorkRequest create(String id) {
      Data inputData = new Data.Builder()
              .putString(TASK_ID, id)
              .build();
      return new OneTimeWorkRequest.Builder(SampleWorker.class)
              .setInputData(inputData)
              .setInitialDelay(24, TimeUnit.HOURS)
              .build();
 }

...

@NonNull
@Override
public Result doWork() {
    String taskId = getInputData().getString(TASK_ID);

    ...
    ...
}

If you still insist on passing Task as parameter to your Worker you can try

 public static OneTimeWorkRequest create(Task task) {
     String strTask = new Gson().toJson(task);
     Data inputData = new Data.Builder()
             .putString(TASK, strTask)
             .build();
     return new OneTimeWorkRequest.Builder(SampleWorker.class)
             .setInputData(inputData)
             .setInitialDelay(24, TimeUnit.HOURS)
             .build();
 }

 ...

 @NonNull
 @Override
 public Result doWork() {
     String strTask = getInputData().getString(TASK);
     Task task = new Gson().fromJson(strTask, Task.class);

    ...
    ...
 }

Add this dependency in build.gradle for Gson

implementation 'com.google.code.gson:gson:2.8.6'

For more information and studies check out here

huangapple
  • 本文由 发表于 2020年9月21日 11:46:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63985945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定