我可以在 AsyncTask 中保留 ContentResolver 多长时间?
我正在尝试编写一个 Activity,它为长时间运行的操作生成 AsyncTask,并定期将状态更新发布回 Activity。我想确保 AsyncTask 能够正确地承受屏幕旋转或 OOM 等其他破坏情况。
我已在 android 开发者列表上阅读了此帖子,反过来 引导我这个实现,这几乎是我所需要的。但是,我的 AsyncTask 的最后一步是从内容解析器中删除一些数据。切勿跳过此步骤,这一点极其重要,因为数据很敏感,在正常情况下不应留在手机上。然而,ContentResolvers 来自上下文,在本例中是 Activity,并且使用此方案,当 AsyncTask 准备好执行删除操作时,Activity 可能为 null。
在 AsyncTask 构造中从 Activity 获取 ContentResolver 并在 Activity 的生命周期内保留它是否安全?如果不是,我该怎么处理这个案子?
I'm trying to write an Activity that spawns off an AsyncTask for a long-running operation, and periodically publishes status updates back to the Activity. I'd like to make sure the AsyncTask survives screen rotation or other destruction conditions like OOM correctly.
I've read this thread on the android-developers list, which in turn led me to this implementation, which is almost what I need. However, the last step of my AsyncTask is to delete some data from a content resolver. It is extremely important that this step is not skipped, because the data is sensitive and should not be left on the phone under normal conditions. However, ContentResolvers come from Contexts, in this case the Activity, and with this scheme the Activity may be null by the time the AsyncTask is ready to do the deletion.
Is it safe to get a ContentResolver from the Activity at AsyncTask construction and hold onto that past the lifetime of the Activity? If not, what can I do to handle this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不从应用程序中获取 ContentResolver ?您需要了解活动和上下文背后的不同概念 - 仅仅因为活动是上下文,并不意味着您需要使用活动来完成它执行的所有任务。仅将 Activity 用于 UI 内容
Why don't you get the ContentResolver from the Application ? You need to understand the different concepts behind an Activity and Context - just because an Activity is a Context, doesn't mean that you need to use an Activity for all tasks that it does. Use an Activity only for UI stuff