如何在Android中高效地使用托管查询?
我试图在与内容提供商合作时使用 android 中的 ManagedQuery() 来取回光标。我使用它是因为有人提到,当活动暂停或恢复时,它将自行处理游标的关闭。
但这仅适用于 Activity 类。但是我有多个后台线程使用android中的asynctask。我在多个线程中对列表进行了相同的获取。所以它的重复代码。这是因为我无法从其他线程 dobackground() 调用一个线程类的函数。有没有一种简单安全的方法来避免这种重复的代码。请提供建议。感谢您的帮助和时间。
I was trying to use the managedquery() in android to get back a cursor when working with a content provider. I used this because it was mentioned that this will handle the closing of the cursor by itself when the activity is paused or resumed.
But however this works on the Activity class only. But I have more than one background threads using the asynctask in android. I do a same fetch of list in more than one thread. So its repetitive code. This is because i cant call a function of one thread class from other thread dobackground(). Is there a easy and safe way to avoid this repetitive code. Kindly advice. Thank you for your help and time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能有一个抽象的 asynctask 类来实现这个数据库查询代码(如果这就是你在后台线程中所做的所有事情,那么将其放入 doInBackground() 中,否则将其放入私有方法中),然后让你的具体扩展此抽象类的子类调用 super.doInBackground 或调用该私有方法?
例如
Can't you have an abstract asynctask class which implements this db query code (if that's all you're doing in the background thread, then put it in doInBackground(), otherwise put it in a private method), and then have your concrete child classes which extend this abstract class call super.doInBackground or call that private method?
E.G.