如何在Android上实现持久队列 [英] How to realize a persistent queue on Android

查看:235
本文介绍了如何在Android上实现持久队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要一个类似于队列的数据结构:将数据放入其中,从中接收数据,类似于FIFO。对于数据,我指的是现在的简单字符串,稍后可能是更复杂的对象。
问题是队列及其内容应该是持久的,无论Android在做什么。如果应用程序关闭并重新打开(甚至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.

我认为队列必须在引擎盖下使用某种存储,最好是设备的内部存储。也许你可以做一些头脑风暴如何实现这一点。队列不一定要在我的应用程序中运行,它也可能是某种丢失耦合的后台服务,如果在Android中可以实现(但对我的应用程序是私有的)。

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).

推荐答案

我建议使用 Sqlite 中的表格。我将创建自己的 SqliteQueue 来实现Queue接口。 商品会在表格中添加条目。 轮询将返回一个条目并将其从表中删除。对于更复杂的对象,您可以使用 GSON 将对象转换为JSON字符串或转换对象的JSON字符串。

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.

这篇关于如何在Android上实现持久队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆