Android JobScheduling - I need to pass an object to my job but how?

Ok - I will answer myself.

It seems that passing more complex objects than String, ints, etc. to a JobService is not possible due to persistence reasons (code could have changed and not classes will not be able to work with old data). That of course makes sense but I think with that restriction it is very difficult to use the JobScheduling.

My "solution" now is saving my object in a File and pass the filename via

PersistableBundle bundle = new PersistableBundle();
bundle.putString("filename", object.getFilename());

In my JobService I am reading that file and am sending it's data via network. Maybe it is not really elegant, but I cannot think of another (better) way. Maybe this helps someone.


I know it is an old question, but I'll leave my solution for whoever finds it useful.

First I turn my object into json using the Google Gson library

MyCustomObject myObj = new MyCustomObject("data1", 123);
Gson g = new Gson();
String json = g.toJson(myObj);

PersistableBundle bundle = new PersistableBundle();
bundle.putString("MyObject", json);

Then you retrieve the string from the bundle and deserialize

String json = params.getExtras().getString("MyObject");
Gson g = new Gson();
MyCustomObject myObj = g.fromJson(json, MyCustomObject.class);