长时间运行Android的“服务” [英] Long running Android 'service'

查看:198
本文介绍了长时间运行Android的“服务”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,其中火活动,在后台运行长时间运行的操作。这些操作与互动活动完成时。我开发一个处理活动/长时间运行的任务耦合组件,照顾活动被破坏并重新创建。

现在,该组件被实现为一个Android服务。活动呼叫bindService并使用所得的IBinder启动和跟踪任务。我决定不使用startService,因为我preFER更丰富的API,可以通过Java接口。

现在的问题。活动一开始起伏,结合服务,并调用serviceApi.runTask(...)。活动A被然后销毁(因为用户翻转手机,例如),并重新为活动A。 A'然后重新绑定到服务,宣布它的存在,一切都应该很好地运行。

除了我的服务也被毁坏。当活动A被破坏,解除绑定的服务。机器人认为不存在更多的客户端,并杀死该服务。当活动A'创建,该服务将再次创造了,我失去了一切旧的服务了。

我可以看到使用的是单为服务的解决方案。然后,它并没有真正必须是一个Android的服务,只是一个实例是向所有人开放。是,Android的非议吗?有没有更好的设计符合这个问题?


Editted:即使我打电话startService,然后绑定到它,没有什么保证服务实例将存在,只要在应用程序运行。机器人可以杀死粘服务,如果资源不足。杀该服务将导致应用程序故障,我不能有。

解决方案
  

即使我打电话startService,然后绑定到它,没有什么保证服务实例将存在,只要在应用程序运行。

正确的。

  

Android的可以杀死粘服务,如果资源是低的。

也是正确的。所有粘的意思是,机器人可能会重新启动该服务。

  

杀该服务将导致应用程序故障,我不能有。

这是不可能的创建是保证运行下去的服务。首先,用户可以摆脱你的服务时,他们想要的,因为用户讨厌谁具有运行永远没有意义的服务的开发人员。写作永远服务只在极少数情况下,必要的;否则,它只是草率的编程。

  

我可以看到使用的是单为服务的解决方案。然后,它并没有真正必须是一个Android的服务,只是一个实例是向所有人开放。是,Android的令人难以接受的?

单身(又名,静态数据成员)将消失时结束处理。这一过程将最终被终止,特别是如果没有活动的服务,没有你的活动是在前台。

I have an Android app, in which Activities fire long running operations that run in the background. These operations interact with the Activities when done. I'm developing a component that handles the Activity/Long-Running-Task coupling, taking care of activities being destroyed and recreated.

Right now that component is implemented as an Android service. The activities call bindService and use the resulting IBinder to start and track tasks. I decided against using startService, because I prefer the richer API possible through a Java interface.

Now the problem. Activity A start ups, binds to the service and calls serviceApi.runTask(...). Activity A is then destroyed (because the user flips the phone, for instance) and recreated as Activity A'. A' then binds again to the service, announces its existence and everything should be running nicely.

Except that my Service gets destroyed. When Activity A is destroyed, it unbinds from the service. Android sees there are no more clients, and kills the service. When Activity A' is created, the service is created again, and I lose everything the old service had.

The only solution I can see is using a singleton for the service. And then it doesn't really have to be an Android service, just an instance that's accessible to everyone. Is that frowned upon in Android? Is there a better design that fits this problem?


Editted: Even if I call startService and then bind to it, nothing guarantees that the service instance will exist as long as the application is running. Android can kill sticky services if resources are low. Killing the service will cause the application to malfunction, and I can't have that.

解决方案

Even if I call startService and then bind to it, nothing guarantees that the service instance will exist as long as the application is running.

Correct.

Android can kill sticky services if resources are low.

Also correct. All "sticky" means is that Android might restart the service.

Killing the service will cause the application to malfunction, and I can't have that.

It is impossible to create a service that is guaranteed to run forever. For starters, users can get rid of your service whenever they want, because users detest developers who have pointless services that run forever. Writing everlasting services is necessary only in very few cases; otherwise, it's just sloppy programming.

The only solution I can see is using a singleton for the service. And then it doesn't really have to be an Android service, just an instance that's accessible to everyone. Is that frowned upon in Android?

Singletons (a.k.a., static data members) will go away when the process is terminated. The process will be terminated eventually, particularly if there are no active services and none of your activities is in the foreground..

这篇关于长时间运行Android的“服务”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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