android - Adding function to foreground service lifecycle methods can create an issue? Is this best practise?
问题描述
I created a timer app. So it's counting time basically and I'm saving the time to room when service close in OnDestroy scope. Is it best practise? Or is there any case that call the lifecycle methods without at user request?
解决方案
请注意,上述方法效率不高/高性能。
这种方法正在消耗宝贵的Android 系统资源,因为当用户导航到另一个应用程序或用户接到电话时,计时的计时器仍在后台运行。
此应用程序可以运行,但 Android 操作系统会在需要更多内存时首先终止此应用程序会话。
这将导致不一致的用户体验和 Play 商店的低评分。
本案例的解决方案是研究实现Activity Lifecycle回调。
onPause()
计时器应该在被调用而不是 on 时立即停止onDestroy()
。
请参考上面的链接并相应地实现所需的回调。这样,您将创建一个高性能且内存友好的应用程序。
推荐阅读
- security - 为什么使用字母数字会话 ID 而不是数字?
- python - 如何从 csv 文件中的文本数据中删除非英语单词(python)
- xamarin - 单击 Xamarin.Forms App 中的导航按钮时系统抛出异常
- linux - 我已经在我的虚拟机CentOS7上安装并配置了Graylog 3.0,在centos7上可以通过IE访问,但在本地无法访问
- c# - 在 Aspnet Core 中附加 HTML 块以发送电子邮件的良好做法
- three.js - 我可以在整个合并的缓冲几何体上应用法线贴图吗?
- r - 根据我的代码,有没有人认为他们可以告诉我是什么导致了这个错误“参数意味着不同的行数:1、0”在 R 中?
- java - 如何创建一个点击更新的 JLabel?
- python - 添加新的子目录而不删除现有的子目录
- ios - 如何根据内容调整文本标签高度