Android 应用程序进程完成时执行方法
Execute method when Android app process is finished
我正在开发一个 Android 应用程序,我需要知道 OS 何时终止进程,以便我可以向我的服务器发出 HTTP 请求。我试图启动一个服务并将请求放在 onDestroy() 方法中,但该服务仅在我手动终止它时停止(使用 START_STICKY)。我使用 START_NOT_STICKY,服务以应用程序结束,但它不执行 onDestroy() 方法。
我的清单如下所示:
<service
android:name=".CheckSessionEndService"
android:enabled="true"
android:exported="false" >
</service>
OnDestroy() 并不总是调用,如果系统终止应用程序。您必须在几个地方调用您的方法 - 在 onDestroy 和 onStop() 以及 onRetainNonConfigurationInstance() 中。但是 onStop() 有更多的机会被调用,如果系统需要释放内存,而不是 onDestroy()。
当进程被kill掉的时候就真的没了。你不能再执行任何事情了。 onDestroy() 在您的服务停止后调用,通过 stopService() 或 stopSelf() (并且服务未绑定),但这并不一定意味着该进程已被终止。如果您担心用户会从最近的应用中刷过您的应用,您可以尝试
或
http://developer.android.com/reference/android/content/pm/ServiceInfo.html#FLAG_STOP_WITH_TASK
我正在开发一个 Android 应用程序,我需要知道 OS 何时终止进程,以便我可以向我的服务器发出 HTTP 请求。我试图启动一个服务并将请求放在 onDestroy() 方法中,但该服务仅在我手动终止它时停止(使用 START_STICKY)。我使用 START_NOT_STICKY,服务以应用程序结束,但它不执行 onDestroy() 方法。
我的清单如下所示:
<service
android:name=".CheckSessionEndService"
android:enabled="true"
android:exported="false" >
</service>
OnDestroy() 并不总是调用,如果系统终止应用程序。您必须在几个地方调用您的方法 - 在 onDestroy 和 onStop() 以及 onRetainNonConfigurationInstance() 中。但是 onStop() 有更多的机会被调用,如果系统需要释放内存,而不是 onDestroy()。
当进程被kill掉的时候就真的没了。你不能再执行任何事情了。 onDestroy() 在您的服务停止后调用,通过 stopService() 或 stopSelf() (并且服务未绑定),但这并不一定意味着该进程已被终止。如果您担心用户会从最近的应用中刷过您的应用,您可以尝试
或
http://developer.android.com/reference/android/content/pm/ServiceInfo.html#FLAG_STOP_WITH_TASK