应用崩溃时如何释放Android资源
我正在开发一个 Android AudioRecord 应用程序。如果我不在脚本末尾调用 AudioRecord 对象上的 release()
方法,则在重新启动手机之前它将无法正确运行。
在处理这个项目时,这成为一个主要麻烦,因为如果应用程序崩溃(有时在测试时发生并且存在意外的缓冲区溢出等),那么我每次都必须重新启动手机。
我有方法在 AudioListener 类的 finalize()
方法中释放缓冲区,并且我还将它们称为主 Activity 的 onStop()
。
当应用程序冻结或崩溃时,这些似乎都不起作用。有没有办法将释放操作附加到错误处理程序或某种通用方法来确保应用程序即使在崩溃后也能执行一些代码以正确释放资源。
感谢您的任何帮助。
I'm working an Android AudioRecord app. If I do not call the release()
method on the AudioRecord object at the end of the script, it will not run correctly until I restart the phone.
This becomes a major hassle when working on this project because if the app should ever crash (which happens sometimes while testing and there's an unexpected buffer overflow, etc.) then I have to restart the phone every time.
I have the method to release the buffers inside the finalize()
method of my AudioListener class, and I also call them onStop()
of the main Activity.
Neither of these seem to work when the app freezes or crashes. Is there a way to attach the release action to an error handler or some general way of ensuring that an app will execute some code to release resources properly even after a crash.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建自己的Application类(需要在AndroidManifest.xml中声明),然后覆盖方法:
更多相关内容可以阅读Android手册中的
Application
Create your own Application class (need to be declared in AndroidManifest.xml) and then overwrite methods:
More about it can be read in Android manuals for
Application