ceran ceran - 1 month ago 11
Java Question

How to realize a persistent queue on Android

My application needs a data structure similar to a queue: put data in it, receive data from it, FIFO-like. With data I mean simple strings for now, later on perhaps more complex objects.
The thing is that the queue and its content should be persistent, regardless of what Android is doing. If the application gets closed and reopened (or even Android reboots), the queue should have the same state and data it had before the application was closed.

I think the queue has to use some kind of storage under the hood, preferably the internal storage of the device. Maybe you could do some brainstorming how to realize this. The queue doesn't necessarily have to run in my application, it could also be some kind of losely coupled background service if that is possible in Android (but private to my application).

Answer

I recommend using a table inside Sqlite. I would create my own SqliteQueue that implements the Queue interface. Offer would add entry to the table. Poll would return an entry and remove it from the table. For more complicated objects you could use GSON to convert the object to a JSON string or convert the JSON string to an object.