应用崩溃时如何释放Android资源

发布于 2024-10-09 04:58:24 字数 405 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-10-16 04:58:24

创建自己的Application类(需要在AndroidManifest.xml中声明),然后覆盖方法:

Application.onLowMemory();
Application.onTerminate();

更多相关内容可以阅读Android手册中的Application

Create your own Application class (need to be declared in AndroidManifest.xml) and then overwrite methods:

Application.onLowMemory();
Application.onTerminate();

More about it can be read in Android manuals for Application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文