Java 在 onStartCommand 中为服务返回什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21941625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:32:06  来源:igfitidea点击:

what to return in onStartCommand for a service

javaandroid

提问by user3240944

I have been looking through the documentation and sometimes the onStartCommand()returns START_NOT_STICKY, sometimes it returns the following:

我一直在查看文档,有时会onStartCommand()返回START_NOT_STICKY,有时会返回以下内容:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return super.onStartCommand(intent, flags, startId);
}

I am now confused as to why some services return super.onStartCommand(intent, flags, startId);

我现在对为什么某些服务返回感到困惑 super.onStartCommand(intent, flags, startId);

采纳答案by Merlevede

It all depends on what you want. The documentationsays:

这一切都取决于你想要什么。该文件说:

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

为了向后兼容,默认实现调用 onStart(Intent, int) 并返回 START_STICKY 或 START_STICKY_COMPATIBILITY。

So returning super.onStartCommand()is equivalent to returning START_STICKY. If you don't want the default behavior you can return another constant.

所以返回super.onStartCommand()相当于返回START_STICKY。如果您不想要默认行为,您可以返回另一个常量。

回答by Aaron Ebinezer

The most often used are

最常用的是

  • Service.START_STICKY
  • Service.START_NOT_STICKY and
  • Service.START_REDELIVER_INTENT
  • 服务。START_STICKY
  • 服务。START_NOT_STICKY 和
  • 服务。START_REDELIVER_INTENT

Service.START_STICKY will restart if the android system terminates for any reason. Service.START_NOT_STICKY will run till it has pending works. Service.START_REDELIVER_INTENT is similar to Service.START_STICKY but the original Intent is re-delivered to the onStartCommand method.

如果 android 系统因任何原因终止,Service.START_STICKY 将重新启动。Service.START_NOT_STICKY 将一直运行,直到它有待处理的工作。Service.START_REDELIVER_INTENT 与 Service.START_STICKY 类似,但原始 Intent 被重新传递给 onStartCommand 方法。

回答by neelkanth_vyas

 Service.START_STICKY
>> the system restarts the service with everything refresh not using the previous intent. 
Service.START_NOT_STICKY 
>> the system does not restarts the service.
Service.START_REDELIVER_INTENT
>>the system restarts the service with using the previous intent.`enter code here