在StartCommand中为服务返回什么

2022-09-02 10:37:05

我一直在查看文档,有时返回,有时它返回以下内容: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);


答案 1

这完全取决于你想要什么。文档说:

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

所以退货就等于回归。如果不需要默认行为,可以返回另一个常量。super.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 方法。


推荐