从最近的应用程序列表中刷出应用程序时,应用程序中的服务是否也已停止?或者,他们还活着吗?

START_STICKY如果系统在onStartCommand()返回后终止服务,则重新创建服务并调用onStartCommand()但不重新传递最后一个意图 . 相反,系统使用null意图调用onStartCommand(),除非有待启动的意图来启动服务 . 在那种情况下,这些意图被交付 . 这适用于不执行命令但无限期运行并等待工作的媒体播放器(或类似服务) .

我知道可以选择在服务的 onStartCommand 上返回START_STICKY . 但是,当我杀死应用程序时,服务不会被杀死吗?它会被退回/重新启动吗?

所以,这意味着如果我返回 START_STICKY ,服务将无限期地继续运行,直到我故意停止它为止?

@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }