android - 如果服务 onDestory() 不能保证被调用,我应该在哪里 release/remove 听众等?
android - if service onDestory() is not guaranteed to be called, where should I release/remove listeners etc?
所以,我有一个 onDestory()
看起来像这样的服务:
@Override
public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
googleApiClient.disconnect();
Foreground.get(this).removeListener(this);
// Here I have some code which sets shared preferences values
phoneDataManager.markLastLocation();
toastHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "service onDestroy", Toast.LENGTH_SHORT).show();
}
});
}
如您所见,我在这里进行了很多操作(断开连接并停止位置更新,在 SQLite 数据库中进行一些更改,并设置一些 SharedPreferences
值。
根据我的阅读,onDestory
不一定会被调用。我看到它没有被调用的一种情况是 -
运行 服务 -> 关闭应用程序(从应用程序列表中删除) -> 我使用 service.startForeground(1, notification);
将服务带到前台,显示通知 -> 单击触发 BroadcastReceiver
:
public class ServiceShutDownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context.getApplicationContext(), LocationService.class);
context.stopService(serviceIntent);
}
}
context.stopService(serviceIntent);
应该 调用 onDestory
但它没有被调用 -> 所以 onDestory
中的所有代码都没有已执行。
这个问题的最佳实践是什么?理想情况下,我想要一段 保证 在服务停止时被调用的代码。
一般来说,您不应该担心 onDestroy()
未被调用的情况,因为这意味着系统正在强制并立即销毁服务(及其所有相关资源和局部变量,保存在内存中).
建议始终将释放持久处理程序(如 MediaPlayer、BroadcastReceiver、GPS 位置更新等)的代码放在这里。这是一个很好的做法,可以处理 90% 的服务正常停止的情况。
但是,如果您想实现一些自定义 "last minute checks",您是对的,系统可能永远不会调用此方法,并且此代码永远不会执行。您的开发策略应该遵循这种模式,而不应该依赖于这种模式,因为没有其他可靠的方法可以知道服务何时将被销毁。
作为最后的手段,您可以监听 Application::onLowMemory() 回调,当系统内存不足并且接近开始终止进程时触发,但即使这样也不能 100% 保证。
所以,我有一个 onDestory()
看起来像这样的服务:
@Override
public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
googleApiClient.disconnect();
Foreground.get(this).removeListener(this);
// Here I have some code which sets shared preferences values
phoneDataManager.markLastLocation();
toastHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "service onDestroy", Toast.LENGTH_SHORT).show();
}
});
}
如您所见,我在这里进行了很多操作(断开连接并停止位置更新,在 SQLite 数据库中进行一些更改,并设置一些 SharedPreferences
值。
根据我的阅读,onDestory
不一定会被调用。我看到它没有被调用的一种情况是 -
运行 服务 -> 关闭应用程序(从应用程序列表中删除) -> 我使用 service.startForeground(1, notification);
将服务带到前台,显示通知 -> 单击触发 BroadcastReceiver
:
public class ServiceShutDownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context.getApplicationContext(), LocationService.class);
context.stopService(serviceIntent);
}
}
context.stopService(serviceIntent);
应该 调用 onDestory
但它没有被调用 -> 所以 onDestory
中的所有代码都没有已执行。
这个问题的最佳实践是什么?理想情况下,我想要一段 保证 在服务停止时被调用的代码。
一般来说,您不应该担心 onDestroy()
未被调用的情况,因为这意味着系统正在强制并立即销毁服务(及其所有相关资源和局部变量,保存在内存中).
建议始终将释放持久处理程序(如 MediaPlayer、BroadcastReceiver、GPS 位置更新等)的代码放在这里。这是一个很好的做法,可以处理 90% 的服务正常停止的情况。
但是,如果您想实现一些自定义 "last minute checks",您是对的,系统可能永远不会调用此方法,并且此代码永远不会执行。您的开发策略应该遵循这种模式,而不应该依赖于这种模式,因为没有其他可靠的方法可以知道服务何时将被销毁。
作为最后的手段,您可以监听 Application::onLowMemory() 回调,当系统内存不足并且接近开始终止进程时触发,但即使这样也不能 100% 保证。