SharedPreferences 未保存在 onDestroy of Service 中
SharedPreferences not saved in onDestroy of Service
在 activity 被杀死之前(当 OnDestroy
被调用时),我在 SharedPreferences
中保存了一些 int
值。
但我注意到,保存值大约需要一秒钟,但应用程序在销毁 Activity
.
之前不会等待它
我可以在它被杀死之前做一些Wait
吗?
这是我的例子:
@Override
public void onDestroy(){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("finalXPos", finalX);
editor.putInt("finalYPos", finalY);
editor.commit();
super.onDestroy();
removeView();
}
======================>> 编辑 <<================== ====
我说错了,说这是给 Activity
的,但事实并非如此。我正在调用 Service 的 onDestroy
,不幸的是没有 onPause
方法可以覆盖那里...
改为按照 the docs, don't rely on the onDestroy()
method to persist your data. Use onPause()
。
Note: do not count on this method being called as a place for saving
data!
For example, if an activity is editing data in a content
provider, those edits should be committed in either onPause() or
onSaveInstanceState(Bundle), not here. This method is usually
implemented to free resources like threads that are associated with an
activity, so that a destroyed activity does not leave such things
around while the rest of its application is still running.
There are situations where the system will simply kill the activity's hosting
process without calling this method (or any others) in it, so it
should not be used to do things that are intended to remain around
after the process goes away.
此外,您可以尝试在 SharedPreferences.Editor
上使用方法 apply()
instead of commit()
,因为它可以更快地异步保存数据,而且根本没有要处理的 return 值。
编辑: 好的,所以你使用的是 Service
。问题是,它们的寿命可能比 Activity
寿命长得多,因为它们在后台保持 运行ning。因此,您的服务 onDestroy()
可能需要很长时间才能被调用,即使您使用后退按钮关闭应用程序也是如此。
我不确定您的应用程序应该如何工作,但我可以提出一些建议。
- 如果您想在用户关闭应用程序时处理
SharedPreferences
,请将该方法移至 Activity
或;
- 在
Activity#onPause()
中,您可以使用 stopService()
方法销毁 Service
。 (参见下面的示例)。
- 如果你的
Service
应该做一些工作,如果在完成这项工作后它应该修改 SharedPreferences
,你可以尝试使用 IntentService
,因为一旦这个某种服务完成了它的工作,它就会自我毁灭。但请注意,一次只能 IntentService
个 运行!它 运行 在 FIFO 队列中。
- 如果您不想使用
IntentService
,您也可以在常规 Service
中调用 stopSelf()
来停止并销毁它。
完成Service
的示例:
@Override
protected void onPause() {
super.onPause();
stopService(new Intent(context, YourService.class));
}
this tutorial 上有关 IntentService
的更多信息。
有关 this link.
的 Service
生命周期的更多信息
onDestroy
in Service
可以通过显式调用 stopService()
方法来实现。
虽然你想清除Service中的Shared Preferences,但这确实不靠谱。我确定会有一个 Activity
启动 Service
.
我会在 Activity
中创建一个方法,就像这样。
private void stopService() {
stopService(new Intent(getApplicationContext(), MyService.class));
}
并且无论何时您想要真正终止该应用程序,您都可以调用 stopService(..)
调用,例如..
private void stopService() {
clearPref();
stopService(new Intent(getApplicationContext(), MyService.class));
}
private void clearPref() {
// Clearing all data from Shared Preferences
editor.clear(); // where editor is an Editor for Shared Preferences
editor.commit();
}
对于Service,需要考虑应用生命周期的多个场景。通常很难想出具体情况,但我相信您一定能够想出办法。祝你好运!
在 activity 被杀死之前(当 OnDestroy
被调用时),我在 SharedPreferences
中保存了一些 int
值。
但我注意到,保存值大约需要一秒钟,但应用程序在销毁 Activity
.
我可以在它被杀死之前做一些Wait
吗?
这是我的例子:
@Override
public void onDestroy(){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("finalXPos", finalX);
editor.putInt("finalYPos", finalY);
editor.commit();
super.onDestroy();
removeView();
}
======================>> 编辑 <<================== ====
我说错了,说这是给 Activity
的,但事实并非如此。我正在调用 Service 的 onDestroy
,不幸的是没有 onPause
方法可以覆盖那里...
改为按照 the docs, don't rely on the onDestroy()
method to persist your data. Use onPause()
。
Note: do not count on this method being called as a place for saving data!
For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running.
There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
此外,您可以尝试在 SharedPreferences.Editor
上使用方法 apply()
instead of commit()
,因为它可以更快地异步保存数据,而且根本没有要处理的 return 值。
编辑: 好的,所以你使用的是 Service
。问题是,它们的寿命可能比 Activity
寿命长得多,因为它们在后台保持 运行ning。因此,您的服务 onDestroy()
可能需要很长时间才能被调用,即使您使用后退按钮关闭应用程序也是如此。
我不确定您的应用程序应该如何工作,但我可以提出一些建议。
- 如果您想在用户关闭应用程序时处理
SharedPreferences
,请将该方法移至Activity
或; - 在
Activity#onPause()
中,您可以使用stopService()
方法销毁Service
。 (参见下面的示例)。 - 如果你的
Service
应该做一些工作,如果在完成这项工作后它应该修改SharedPreferences
,你可以尝试使用IntentService
,因为一旦这个某种服务完成了它的工作,它就会自我毁灭。但请注意,一次只能IntentService
个 运行!它 运行 在 FIFO 队列中。 - 如果您不想使用
IntentService
,您也可以在常规Service
中调用stopSelf()
来停止并销毁它。
完成Service
的示例:
@Override
protected void onPause() {
super.onPause();
stopService(new Intent(context, YourService.class));
}
this tutorial 上有关 IntentService
的更多信息。
有关 this link.
Service
生命周期的更多信息
onDestroy
in Service
可以通过显式调用 stopService()
方法来实现。
虽然你想清除Service中的Shared Preferences,但这确实不靠谱。我确定会有一个 Activity
启动 Service
.
我会在 Activity
中创建一个方法,就像这样。
private void stopService() {
stopService(new Intent(getApplicationContext(), MyService.class));
}
并且无论何时您想要真正终止该应用程序,您都可以调用 stopService(..)
调用,例如..
private void stopService() {
clearPref();
stopService(new Intent(getApplicationContext(), MyService.class));
}
private void clearPref() {
// Clearing all data from Shared Preferences
editor.clear(); // where editor is an Editor for Shared Preferences
editor.commit();
}
对于Service,需要考虑应用生命周期的多个场景。通常很难想出具体情况,但我相信您一定能够想出办法。祝你好运!