Android:服务被终止后,我们如何才能保持服务状态以供以后恢复? [英] Android: When a service is killed, how can we persist the service state for later restoration?

查看:126
本文介绍了Android:服务被终止后,我们如何才能保持服务状态以供以后恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个应用,该应用实质上是具有某些装饰功能的计时器/秒表.我们定义了一项服务,它可以打勾时钟并提醒订阅某些计时器事件的侦听器(活动等).

We have created an app that is essentially a timer/stopwatch with some decked out features. We defined a service that ticks the clock and alerts listeners (activities, etc.) who are subscribed to certain timer events.

我们希望能够在Android终止我们的服务(用于内存回收)时保存计时器/秒表的状态(经过的秒数,到下一个事件的时间,用户提供的配置等),然后恢复状态用户恢复应用时的服务状态. 对我们来说,这意味着可能要保存和还原对象及其状态.我们的服务内容很多.基本上,我们的服务由我们所有的计时器模型组成,当服务终止时,所有计时器模型都会终止.

We would like to be able to save the state of the timer/stopwatch (elapsed seconds, time until next event, user-provided config etc.) whenever android kills our service (for memory recollection), and then restore the state of the service when the app is restored by the user. For us, this means possibly preserving and restoring objects and their state. We have lots of composition in our service. Basically, our service is composed of all our timer models, of which die when the service dies.

有哪些策略可以保持服务状态? PreferencesManager可能不够强大,尽管不能达到我们的目的.我们可以依靠Service onDestroy()方法保存状态吗(例如在SQLite中)?如果android决定终止我们的进程,是否可以保证将调用服务onDestroy()?

What strategies are there to persist the state of the service? PreferencesManager probably isn't robust enough for our purposes, though it might be. Can we rely on the Service onDestroy() method to save state (say in SQLite)? If android decides to kill our process, is it even guaranteed that the service onDestroy() will be called?

谢谢!

推荐答案

我们定义了一项服务,它可以滴答时钟并提醒订阅某些计时器事件的侦听器(活动等)

We defined a service that ticks the clock and alerts listeners (activities, etc.) who are subscribed to certain timer events

您不需要一直在运行服务,只需看着时钟滴答作响即可.这种方法是用户敌对的,因为它没有充分的理由占用系统RAM.使用AlarmManager可以控制某些计时器事件",并在其余时间内将服务保持在内存之外.

You do not need to have a service running all the time, watching the clock tick. This approach is user-hostile, as it ties up system RAM for no good reason. Use AlarmManager to get control at "certain timer events", and keep your service out of memory for the rest of the time.

我们希望能够保存计时器/秒表的状态(经过的秒数,到下次事件的时间,用户提供的配置等)

We would like to be able to save the state of the timer/stopwatch (elapsed seconds, time until next event, user-provided config etc.)

不要持续经过的秒数".保持计时器/秒表开始的时间.那么经过的秒数就是减法的问题.

Do not persist "elapsed seconds". Persist the time when the timer/stopwatch began. Elapsed seconds is then a matter of subtraction.

不要坚持直到下一次事件的时间".持续下一个事件的时间 .

Do not persist "time until next event". Persist the time of the next event.

这些值不会连续变化,因此可以在启动计时器或计时器生命周期中的其他主要生命周期事件时保留.

These values do not continuously change, and therefore can be persisted when the timers are started or other major lifecycle events in the life of the timer.

有哪些策略可以保持服务状态?

What strategies are there to persist the state of the service?

使用数据库,SharedPreferences或您自己选择的文件格式.

Use a database, SharedPreferences, or a file format of your own choosing.

我们可以依靠Service onDestroy()方法保存状态(例如在SQLite中)吗?

Can we rely on the Service onDestroy() method to save state (say in SQLite)?

否.

如果android决定终止我们的进程,是否甚至可以保证将调用服务onDestroy()?

If android decides to kill our process, is it even guaranteed that the service onDestroy() will be called?

不保证会被调用.

这篇关于Android:服务被终止后,我们如何才能保持服务状态以供以后恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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