当它自己重新启动时,在 IntentService 中将相同的变量传递给自己

Pass the same variables to itself in an IntentService when it restarts itself

我有一个 IntentService 需要在它被销毁时重新启动。
最初我从 protected void onHandleIntent(Intent intent) {...}

中的 Intent 获取所有变量 变量在服务运行期间发生变化。一旦我关闭(销毁)应用程序,服务就会停止,但我希望它继续 运行 直到我告诉它停止。

要重新启动 IntentService,我调用:

return START_REDELIVER_INTENT;

但变量的值被重置为原始值。但是我需要 IntentService 在重新启动之前的值。

所以我的问题是:
在调用 return START_REDELIVER_INTENT; 时是否可以更改传递给 IntentService 的意图?

或者有没有其他方法可以保存变量,以便在重新启动的 IntentService 中重新使用变量?

你可以在这个地方使用偏好。 当您使用 Intent 发送数据时,优先存储该数据。

此数据将存储在 phone 内存中。因此,当您更新或卸载该应用程序时,它将存储到那个时候。所以你不需要面对这种可能性。

在您需要该数据时调用首选项。

示例http://www.tutorialspoint.com/android/android_shared_preferences.htm