android 如何将复杂类型的对象传递给 WorkManager。即列表、地图、POJO
How to pass Complex Types of Objects to WorkManager in android. i.e lists, maps, POJO
WorkManager 是一个用于排队工作的库,保证在满足其约束后执行。 WorkManager 允许观察工作状态并能够创建复杂的工作链。
支持的有效类型仅为:Boolean、Integer、Long、Double、String 以及每种类型的数组版本。
// 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;
}
}
我如何传递复杂类型,例如:
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中解码。
已经使用了 gson,因为它已经是这个应用程序的依赖项。
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);
Save it in database or you can use JsonStore from IBM, then use rxjava to fetch it, and on successful fetching just call your doWork thing you specified there. Simple.
WorkManager 是一个用于排队工作的库,保证在满足其约束后执行。 WorkManager 允许观察工作状态并能够创建复杂的工作链。
支持的有效类型仅为:Boolean、Integer、Long、Double、String 以及每种类型的数组版本。
// 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;
}
}
我如何传递复杂类型,例如:
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中解码。
已经使用了 gson,因为它已经是这个应用程序的依赖项。
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);
Save it in database or you can use JsonStore from IBM, then use rxjava to fetch it, and on successful fetching just call your doWork thing you specified there. Simple.