代码之家  ›  专栏  ›  技术社区  ›  Joshua

如何在workmanagerforandroid中创建带有参数的Worker?

  •  5
  • Joshua  · 技术社区  · 6 年前

    Android架构有一个新的组件 WorkManager .

    example ,

    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 ?

    1 回复  |  直到 6 年前
        1
  •  63
  •   Dhaval Patel    6 年前

    你可以用 setInputData

    /***  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 .

        2
  •  15
  •   Pratik Butani Umesh N    5 年前

    在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
  •  -1
  •   Vaibhav Pallod    4 年前

    当我使用WorkManager并试图向它传递一些对象时

    Data data = new Data.Builder().put("passstring",object).build();
    

    getting me Builder.put只能从同一个库组(groupId=androidx.work)中调用错误 你有解决办法吗?