package com.maniu.mn_vip_jetpack_other.workmanager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class MyWorkManager4 extends Worker {
    public static final String TAG = MyWorkManager4.class.getSimpleName();
    private Context context;
    private WorkerParameters workerParams;

    public MyWorkManager4(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.context = context;
        this.workerParams = workerParams;
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.e(TAG, "doWork: 后台任务执行了");

        // 接收Activity传递过来的数据
        final String dataString = workerParams.getInputData().getString("data");
        Log.e(TAG, "doWork: 接收Activity传递过来的数据:" + dataString);

        // 反馈数据 给 Activity
        // 把任务中的数据回传到activity中
        Data outputData = new Data.Builder().putString("data", "执行完任务将结果和数据回传给Activity").build();
        @SuppressLint("RestrictedApi")
        Result.Success success = new Result.Success(outputData);

        // return new Result.Failure(); // 本地执行 doWork 任务时 失败
        // return new Result.Retry(); // 本地执行 doWork 任务时 重试
        // return new Result.Success(); // 本地执行 doWork 任务时 成功 执行任务完毕
        return success;
    }
}
