zoukankan      html  css  js  c++  java
  • 处理Service的onStartCommand返回值

    onStartCommand() 方法必须返回整型数,用于描述系统应该如何在服务终止的情况下继续运行服务.

    从 onStartCommand() 返回的值必须是以下常量之一:

    • START_NOT_STICKY
      如果系统在 onStartCommand() 返回后终止服务,则除非有挂起 Intent 要传递,否则系统 不会重建服务
      这是最安全的选项,可以 避免在不必要时以及应用能够轻松重启所有未完成的作业时运行服务。
    • START_STICKY
      如果系统在 onStartCommand() 返回后终止服务,则会 重建服务并调用 onStartCommand(),但绝对不会重新传递最后一个 Intent.
      相反,除非有挂起 Intent 要启动服务(在这种情况下,将传递这些 Intent ),否则系统会通过 空Intent 调用 onStartCommand()。
      这适用于 不执行命令、但无限期运行并等待作业的媒体播放器(或类似服务)。
    • START_REDELIVER_INTENT
      如果系统在 onStartCommand() 返回后终止服务,则会 重建服务,并通过 传递给服务的最后一个 Intent 调用 onStartCommand()。
      任何挂起 Intent 均依次传递。这适用于主动执行应该立即恢复的作业(例如下载文件)的服务。

    小结:

      1. START_STICKY,START_REDELIVER_INTENT 会重启服务
      2. START_STICKY 会传递null的intent
      3. START_REDELIVER_INTENT 会传递最后一个intent
  • 相关阅读:
    Composite in Javascript
    Model Validation in Asp.net MVC
    HttpRuntime.Cache vs. HttpContext.Current.Cache
    Controller Extensibility in ASP.NET MVC
    The Decorator Pattern in Javascript
    The Flyweight Pattern in Javascript
    Model Binding in ASP.NET MVC
    Asp.net MVC
    jQuery Ajax 实例 全解析
    ASP.NET AJAX入门系列
  • 原文地址:https://www.cnblogs.com/mjblogs/p/5090987.html
Copyright © 2011-2022 走看看