web-dev-qa-db-ja.com

WorkManager for Androidでパラメーターを使用してワーカーを作成する方法は?

Androidアーキテクチャには、新しいコンポーネント WorkManager があります。

から、

class CompressWorker(context : Context, params : WorkerParameters)
    : Worker(context, params) {

    override fun doWork(): Result {

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."
        myCompress()

        // Indicate success or failure with your return value:
        return Result.SUCCESS

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)

    }

}

val compressionWork = OneTimeWorkRequestBuilder<CompressWorker>().build()

コンストラクターまたはWorkerのパラメーターを受け入れるdoWorkを作成するにはどうすればよいですか?

11
Joshua

setInputDataメソッドを使用して、Bundleと同様にデータを送信できます。

/***  Logic to set Data while creating worker **/
val compressionWork = OneTimeWorkRequest.Builder(CompressWorker::class.Java)
val data = Data.Builder()
//Add parameter in Data class. just like bundle. You can also add Boolean and Number in parameter.
data.putString("file_path", "put_file_path_here")
//Set Input Data
compressionWork.setInputData(data.build())
//enque worker
WorkManager.getInstance().enqueue(compressionWork.build())


/*** Logic to get Data  ***/
class CompressWorker(context : Context, params : WorkerParameters)
    : Worker(context, params) {

    override fun doWork(): Result {

        //get Input Data back using "inputData" variable 
        val filePath =  inputData.getString("file_path")

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."
        myCompress()

        // Indicate success or failure with your return value:
        return Result.SUCCESS

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)

    }

}

詳細については、こちらをご覧ください link

22
Dhaval Patel

Javaの場合:

次のようにパラメーターを渡します。

    Constraints.Builder builder = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED);

    // Passing params
    Data.Builder data = new Data.Builder();
    data.putString("SyncMaster", syncModuleName);

    OneTimeWorkRequest syncWorkRequest =
            new OneTimeWorkRequest.Builder(SyncWorker.class)
                    .addTag("Sync")
                    .setInputData(data.build())
                    .setConstraints(builder.build())
                    .build();

    WorkManager.getInstance().enqueue(syncWorkRequest);

次のように取得できます。

public class SyncWorker extends Worker {

    private static final String TAG = "MyWorker";

    public SyncWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        mContext = context;
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d(TAG, "doWork for Sync");
        String syncTable = getInputData().getString("SyncMaster");
        return Result.success();
    }
}

それが明らかに役立つことを願っています。

3
Pratik Butani