1

I am trying to add extras to my JobInfo.Builder supporting Android API 23 and higher. Thing is when adding Bundle to builder then I need to do builder.setTransientExtras.

ComponentName serviceComponent = new ComponentName(context, RestApiJobService.class);

JobInfo.Builder builder = new JobInfo.Builder(getUniqueJobId(), serviceComponent);

builder.setTransientExtras(RestApiJobFactory.save(jobItem);

The issue here is that builder.setTransientExtras is only supported in API 26 and higher. And builder.setExtras only supports PersistableBundle. And I need it to support Bundle since I have Files in my Bundle.

Is there any workaround to support builder.setTransientExtras in Android 23 and also the same for getTransientExtras.

Or maybe there is a way to use Bundle with bundle.setExtras().

dr_g
  • 1,179
  • 7
  • 10
Richard
  • 1,087
  • 18
  • 52

1 Answers1

1

Using builder.setTransientExtras can throw an IllegalArgumentException when build() is called on the JobInfo.Builder for persisted jobs. Safer to use PersistableBundle and restrict the content of the bundle to persistable types, e.g. Long, Double, String and nested PersistableBundle plus others. If you can modify the RestApiJobFactory class, instead of returning a Bundle that passes around File objects, try storing the File locations as String. If you can't, write a helper method to convert your Bundle to a PeristableBundle, extracting the File info in the process. See this answer https://stackoverflow.com/a/45178007/949224 for an example.

The principle for persisted jobs is to reduce the extras down to basic peristable types, and when in the job itself is running, the complex transient types should be constructed.

dr_g
  • 1,179
  • 7
  • 10
  • I will look into converting the bundle, but storing files as `file.absolutePath` is what I ended up doing for now and it seems to work well (have not tried it with `Persistablebundle yet because I have a list of those locations). The last issue I have is that I have a really big object that is `Parcelable` and I am passing it with `putParcelable` and `getParcelable`. Is it safe to convert `Parcelable` and an `ArrayList` with the method that you sent? – Richard Jan 09 '20 at 10:51
  • You're on the right track from what I can gather. You can create an appropriate key and save `file.absolutePath` in the `PersistableBundle`. On second question, how big is this Parcelable object? – dr_g Jan 09 '20 at 10:54
  • How do I answer that how big it is. It has multiple `Parcelable` objects inside of it, which have their own `Parcelable` objects and I would say it is rather big. Around 6 `Objects` inside of the first one and two of them have their own `objects` inside which each have one more `object` inside. – Richard Jan 09 '20 at 10:57
  • You should be fine. It only really becomes an issue with thousands of nested persistable objects.There is a limit set on for the internal service binder transaction buffer of 1MB by default. See https://developer.android.com/reference/android/os/TransactionTooLargeException.html. – dr_g Jan 09 '20 at 11:53
  • Well thank you for your answer, I will try to implement it tomrrow or next week and let you know if I got it working. Since the bounty is expiring I will give it to you and hope it works. – Richard Jan 09 '20 at 11:55
  • well it seems that it is not converting my `ArrayList` to `PersistableBundle` nor the `Parcelable` Object. I suppose that maybe If I did `String[]` of image paths' it would persist that data, but the `Parcelable object` is still the problem. – Richard Jan 16 '20 at 11:34
  • 1
    By converting the `Parcelable` into `JSON String` first and then saving putting it as a `String` into the `Bundle` worked, so i got it working – Richard Jan 16 '20 at 12:30