Ad
How To Send A ByteArray To Worker Class When Implementing WorkManger?
I am trying to send a byteArray as an input data to my Worker Class inside the WorkRequest. Is there a way to send byteArray and receive the same inside the worker class?
I have tried to send IntArray and I am able to achieve the result.
Implementation:
//work request
val workRequest = OneTimeWorkRequest.Builder(MyWroker::class.java)
.setInputData(createInputData())
.build()
//create Input Data for work request
fun createInputData(): Data {
return Data.Builder()
.putString(FIRST_KEY, "My value")
.putInt(SECOND_KEY, 5)
.putByteArray(getByteArray())
.build()
}
I am getting an error cannot resolve method getByteArray(). I have read the documentation of Data class and there is no such method available.
Ad
Answer
The support to store and retrieve bytes and byte arrays into Data
object has been added to WorkManager v2.1.0-alpha01.
You can add WorkManager's KTX and use OneTimeWorkRequestBuilder<>()
and workDataOf()
in your work request (or use the putByteArray()
method of the Data.Builder
object):
val workRequest = OneTimeWorkRequestBuilder<MyWorker>()
.setInputData(createInputData())
.build()
private fun createInputData() = workDataOf(
FIRST_KEY to "My value",
SECOND_KEY to byteArrayOf(0x2E, 0x38))
and then retrieve the byte array in your worker using something like:
class MyWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params) {
override fun doWork(): Result {
val myByteArray = inputData.getByteArray()
// Do something with the ByteArray
Result.success()
}
}
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad