【问题标题】:what to return in onStartCommand for a service在 onStartCommand 中为服务返回什么
【发布时间】:2014-02-21 17:50:12
【问题描述】:

我一直在查看文档,有时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);
}

我现在很困惑为什么有些服务会返回super.onStartCommand(intent, flags, startId);

【问题讨论】:

    标签: java android


    【解决方案1】:

    这一切都取决于你想要什么。 documentation 说:

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

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

    【讨论】:

    • 谢谢你的回答。我有一个问题。如果我没有在我的服务中覆盖onStartCommand(),那么默认情况下会返回什么值?系统将如何处理我的服务以重新启动/停止??
    • 我认为它会返回START_STICKY
    【解决方案2】:

    最常用的是

    • Service.START_STICKY
    • Service.START_NOT_STICKY 和
    • Service.START_REDELIVER_INTENT

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

    【讨论】:

      【解决方案3】:
       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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-06
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 2020-02-02
        相关资源
        最近更新 更多