START_STICKY和START_NOT_STICKY [英] START_STICKY and START_NOT_STICKY

查看:3868
本文介绍了START_STICKY和START_NOT_STICKY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一的区别是什么href="http://developer.android.com/reference/android/app/Service.html#START_STICKY"><$c$c>START_STICKY和<一href="http://developer.android.com/reference/android/app/Service.html#START_NOT_STICKY"><$c$c>START_NOT_STICKY而在Android的实施服务?任何人都可以指出一些标准的例子..?

What is the difference between START_STICKY and START_NOT_STICKY while implementing services in android? Could anyone point out to some standard examples.. ?

推荐答案

这两个codeS只与当手机运行的内存并杀死该服务之前,完成执行。 START_STICKY告诉操作系统重新创建服务后,它有足够的内存和调用onStartCommand()再次空意图。 START_NOT_STICKY告诉操作系统懒得再重新创建该服务。还有第三个code START_REDELIVER_INTENT告诉操作系统来重新创建服务及返还相同的意向onStartCommand()。

Both codes are only relevant when the phone runs out of memory and kills the service before it finishes executing. START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent. START_NOT_STICKY tells the OS to not bother recreating the service again. There is also a third code START_REDELIVER_INTENT that tells the OS to recreate the service AND redelivery the same intent to onStartCommand().

此文章由戴安娜Hackborn解释这个好多了,然后官方文档的背景。

This article by Dianne Hackborn explained the background of this a lot better then the official documentation.

来源:<一href="http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html">http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html

这里的关键部分是由函数返回一个新的结果code,   系统知道它应该做的,如果它的进程服务   在运行过程中被杀害:

The key part here is a new result code returned by the function, telling the system what it should do with the service if its process is killed while it is running:

START_STICKY是基本相同的previous行为,其中该   服务是左开始,将以后由系统重新启动。   从该平台的previous版本唯一的区别是,它   如果它被重新启动,因为它的进程被终止,onStartCommand()   将调用该服务的下一个实例与空意图   而不是没有被调用的。使用这种模式的服务应该   经常检查这种情况,并处理它恰如其分。

START_STICKY is basically the same as the previous behavior, where the service is left "started" and will later be restarted by the system. The only difference from previous versions of the platform is that it if it gets restarted because its process is killed, onStartCommand() will be called on the next instance of the service with a null Intent instead of not being called at all. Services that use this mode should always check for this case and deal with it appropriately.

START_NOT_STICKY说,从onStartCreated)返回(如果后   该进程被终止与没有剩余启动命令来实现,   然后该服务将被停止,而不是重新启动。这使得一个   为服务更多的意义上,意在仅运行,而   执行发送给他们的命令。例如,一个服务可以开始   从报警每15分钟来轮询一些网络状态。如果它获得   死亡,而这样做的工作,这将是最好的就让它是   停下来,让下一次启动了火警。

START_NOT_STICKY says that, after returning from onStartCreated(), if the process is killed with no remaining start commands to deliver, then the service will be stopped instead of restarted. This makes a lot more sense for services that are intended to only run while executing commands sent to them. For example, a service may be started every 15 minutes from an alarm to poll some network state. If it gets killed while doing that work, it would be best to just let it be stopped and get started the next time the alarm fires.

START_REDELIVER_INTENT就像START_NOT_STICKY,但如果   它调用stopSelf()对于一个给定前服务的过程中被杀害   意图,该意图将被重新传送到它,直到它完成   (除非多尝试一些号码后仍不能完成的,在   而此时系统给出了)。这对于服务,这是有用   工作接收命令的事,并希望确保他们这样做   最终完成的工作,为每个发送的命令。

START_REDELIVER_INTENT is like START_NOT_STICKY, except if the service's process is killed before it calls stopSelf() for a given intent, that intent will be re-delivered to it until it completes (unless after some number of more tries it still can't complete, at which point the system gives up). This is useful for services that are receiving commands of work to do, and want to make sure they do eventually complete the work for each command sent.

这篇关于START_STICKY和START_NOT_STICKY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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