应用程序崩溃的内务管理
如果 Android 应用程序因某种原因崩溃,我可以在某个地方执行一些内务活动吗?有些东西比如关闭手柄、连接等?
I there a place where I can do some housekeeping activities if an Android application crashes for some reason? Some things like closing handles, connections etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Thread.setDefaultUncaughtExceptionHandler() 来注册崩溃事件。不过,如果您的整个应用程序崩溃,您不必担心关闭连接,因为它们都会与应用程序一起被杀死。
You can use Thread.setDefaultUncaughtExceptionHandler() to register for crash events. Though if your entire app crashes you don't have to worry about closing connections as they will all be killed along with the app.
操作系统内核将关闭所有打开的文件句柄、套接字、分配的内存等。它无法刷新程序内部的缓冲 IO,并且只会拆除 TCP 套接字,而不会向远程对等点说
BYE
,但是您可以(或需要)的东西不多当操作系统获取您的进程时执行此操作。但是,如果您的应用程序崩溃并在持久存储上留下不一致的数据,您可能需要在下次启动时进行一些清理。
The OS kernel will close all your open filehandles, sockets, allocated memory, and so forth. It can't flush buffered IO internal to the program, and it'll just tear down your TCP sockets without saying
BYE
to the remote peer, but there isn't much you can (or need) to do when the OS reaps your process.But you might need to do some cleanup at next startup, if your application crashed while leaving inconsistent data on the persistent storage.