在保持方向更改运行服务 [英] Keeping a service running during orientation changes

查看:94
本文介绍了在保持方向更改运行服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有一个服务它负责寻找蓝牙连接到外部设备后。这服务类定期轮询外部蓝牙设备的数据,并补充说,最新的数据到一个日志缓存中(也可能是SD卡)的内存。

In my application, I have a Service which is responsible for looking after a Bluetooth connection to an external device. This Service class periodically polls the external Bluetooth device for data and adds that latest data into a log held in cache (or possibly SD card) memory.

在各类活动,我有,有一个特殊的活动的再presents类主界面。它负责显示,以图形的形式,根据该高速缓存文件的数据记录的数据。让我们把这个活动控制台。用户可以来回滚动上图审查收集并记录在缓存中,因为应用程序启动的数据。

Amongst the various Activity classes that I have, there is one particular Activity that represents the main UI. It is responsible for displaying, in graphical form, the logged data based upon the cache file data. Let's call this Activity the Dashboard. The user can scroll back and forth on that graph to review the data collected and logged within the cache since the application was started.

有关这个问题的目的,有两种操作模式来考虑。这是可能的用户选择了登录到SD卡选项,从而使应用程序必须继续进行投票,并记录到SD卡上,即使所有的活动类杀 (例如,用户已经回到了发射器)。在这种情况下,我的服务开始使用 .startService(),并继续运行,并且只被停止时,用户再次调用该应用程序,并禁用SD卡记录。另一种模式是,用户没有选择登录到SD卡,在这种情况下,服务还是管理蓝牙连接,查询和记录到高速缓存内存视觉显示数据在图表上,但只有目的需要这样做,而控制台 活动正在使用。

For the purpose of this question, there are two modes of operation to consider. It is possible for the user to select a "log to SD card" option, whereby the application must continue the polling and logging to SD card even when all Activity classes are killed' (e.g. the user has gone back to the launcher). In this case my Service is started using .startService() and continues to run, and will only be stopped when the user invokes the application again and disabled SD card logging. The other mode is where the user hasn't selected "log to SD card", in which case the Service is still managing the Bluetooth connection, polling and logging to cache memory for the purpose of visually displaying the data on the graph, but only needs to do so while the Dashboard Activity is being used.

我目前所面对的是,控制台 活动最初绑定到服务使用 bindService(),并做了相应的调用 unbindService()的onPause()办法(否则我当然会泄漏服务)。

What I have at the moment is that the Dashboard Activity initially binds to the Service using bindService(), and does a corresponding call to unbindService() within the onPause() method (as otherwise I would of course leak the Service).

的问题是,服务需要维护的蓝牙连接,并在方向改变或当用户调用另一个活动过顶(如检查电子邮件)。现在,如果用户选择登录到SD卡,从而调用 startService()那当然是没有问题的。当然,问题是如何将一个活动区分被破坏,然后由于定向(或其他配​​置)的变化再次创建,被销毁,因为在用户回到该发射器。在前一种情况下,我不想让服务数据记录已经被中断。在后一种情况下,我想服务停止,如果的用户没有选择登录到SD卡。

The problem is that the Service needs to maintain the Bluetooth connection and continue logging during orientation changes or when the user invokes another Activity over the top (e.g. checks an email). Now if the user has selected "log to SD card" resulting in a call to startService() then of course there is no problem. The problem of course is how to distinguish between an Activity being destroyed and then created again due to orientation (or some other configuration) change, and being destroyed because the user went back to the launcher. In the former case, I don't want the Service datalogging to have been interrupted. In the latter case, I want the Service to stop, if the user hasn't selected "log to SD card".

最好的解决办法我能想到的这目前是服务是的总是的开始使用 startService(),使它继续当控制台已被破坏运行。我会那么做的是实现一个超时服务里,由此服务将停止自己的除非的连续SD卡启用了日志,控制台的onCreate ð再次在五秒内(比方说),并重新绑定到服务。这似乎有点粗,我不禁想到这必须是一个常见的​​设计问题,有一个更好的解决办法,我已经忽略了。

The best solution I can think of for this at the moment is for the service to be always started using startService(), so that it continues to run when the Dashboard has been destroyed. What I would then do is implement a time-out within the Service, whereby the Service will stop itself unless continuous SD card logging is enabled, or the Dashboard is onCreated again within five seconds (say) and re-binds to the Service. This seems a little crude, and I can't help thinking this must be a common design problem that has a better solution that I've overlooked.

推荐答案

您可以使用一种方法,是检查是否活动 isFinishing()之前解除绑定在你的的onPause()。如果完成,你会推迟解除绑定到你的的onDestroy()方法。在的onDestroy()叫,但是,您可能会持续在 onRetainNonConfigurationInstance()方法,你ServiceConnection。如果你这样做执行的持久性,比你不会真的叫取消绑定()在里面你的的onDestroy()在所有的,只是让你活动的新实例做解除绑定。

One approach you could use, is check to see if the Activity isFinishing() before unbinding in your onPause(). If it is finishing, you would defer the unbinding to your onDestroy() method. Before onDestroy() is called, however, you could persist your ServiceConnection in the onRetainNonConfigurationInstance() method. If you do perform that persistence, than you wouldn't actually call unBind() inside of on your onDestroy() at all and simply let the new instance of your Activity do the unbinding.

话虽这么说,这取决于你的应用程序,你会发现它只是更容易启动/停止服务。

That being said, depending on your application, you might find it is just easier to start/stop your service.

这篇关于在保持方向更改运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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