gpt4 book ai didi

android - START_STICKY 和 START_NOT_STICKY

转载 作者:IT老高 更新时间:2023-10-28 12:51:24 29 4
gpt4 key购买 nike

START_STICKY 和有什么区别?和 START_NOT_STICKY在android中实现服务时?谁能指出一些标准的例子..?

最佳答案

这两个代码仅在手机内存不足并在服务完成执行之前终止服务时才相关。 START_STICKY 告诉操作系统在它有足够的内存后重新创建服务,并以空 Intent 再次调用 onStartCommand()START_NOT_STICKY 告诉操作系统不要再费心重新创建服务。还有第三个代码 START_REDELIVER_INTENT 告诉操作系统重新创建服务并将相同的 Intent 重新传递给 onStartCommand()

Dianne Hackborn 的这篇文章比官方文档更好地解释了这个背景。

来源:http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html

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 processis killed while it is running:

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

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

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

关于android - START_STICKY 和 START_NOT_STICKY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9093271/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com