何将复杂类型的对象传递给android中的WorkManager

何将复杂类型的对象传递给android中的WorkManager

本文介绍了如何将复杂类型的对象传递给android中的WorkManager.即列表,地图,POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WorkManager是一个用于使工作入队的库,该库在满足其约束条件后可以执行. WorkManager允许观察工作状态以及创建复杂工作链的能力.

仅支持有效类型:布尔,整数,长整数,双精度,字符串和每种类型的数组版本.

// Define the Worker class:
public class MathWorker extends Worker {

    // Define the parameter keys:
    public static final String KEY_X_ARG = "X";
    public static final String KEY_Y_ARG = "Y";
    public static final String KEY_Z_ARG = "Z";
    // ...and the result key:
    public static final String KEY_RESULT = "result";

    public MathWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
        super(context, params);
    }

    @Override
    public Worker.Result doWork() {
        // Fetch the arguments (and specify default values):
        int x = getInputData().getInt(KEY_X_ARG, 0);
        int y = getInputData().getInt(KEY_Y_ARG, 0);
        int z = getInputData().getInt(KEY_Z_ARG, 0);

        // ...do the math...
        int result = myCrazyMathFunction(x, y, z);

        //...set the output, and we're done!
        Data output = new Data.Builder()
            .putInt(KEY_RESULT, result)
            .build();
        setOutputData(output);
        return Result.SUCCESS;
    }
}

如何传递复杂的类型,如:

How can i pass complex types like:

Map<String, Object> studListMap = new HashMap<>();
        studListMap.put("list", studentDetails);

        // Create the Data object:
        @SuppressLint("RestrictedApi")
        Data myData = new Data.Builder()
                .put(KEY_STUD_LIST_ARG,studListMap)
                .build();

        //set network required
        Constraints myConstraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

推荐答案

如果可能,您可以序列化POJO.例如,如果确实很小又简单,则可以使用JSON将其编码为字符串,然后在Worker中对其进行解码.

If possible, you may serialize your POJO. For example, if it is truly small and simple, you can use JSON to encode it to string and then decode it in the Worker.

已使用gson,因为它已经是此应用程序中的依赖项.

Have used gson, since it's already a dependency in this app.

 Map<String, Object> studListMap = new HashMap<>();
        studListMap.put("list", studentDetails);

        Type listOfStudObject = new TypeToken<List<StudentDetails>>(){}.getType();
        String s = gson.toJson(studentDetails, listOfStudObject);

        // Create the Data object:
        @SuppressLint("RestrictedApi")
        Data myData = new Data.Builder()
                .putString(KEY_STUD_LIST_ARG,s)
                .build();

        //set network required
        Constraints myConstraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build();

        // ...then create and enqueue a OneTimeWorkRequest that uses those arguments
        OneTimeWorkRequest attendWork = new OneTimeWorkRequest.Builder(AddAttendanceWorker.class)
                .setConstraints(myConstraints)
                .setInputData(myData)
                .build();

        WorkManager.getInstance()
                .enqueue(attendWork);

这篇关于如何将复杂类型的对象传递给android中的WorkManager.即列表,地图,POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:12