怎样在子线程中操作数据库自己写的待办事项功能,希望能在距离具体设置时间的某个时间段给用户提醒。(比方说设置该事项1月2号,希望在1月1号某个时间提醒)。
我个人的思路是,建一个service,然后用ContentObserve监听数据库,为了效率考虑,想新建一个子线程完成。 
问题是:想在子线程中查询数据库,但在子线程调用managedQuery方法时会在获取相应的contentResolver时抛出空指针异常。想问问应该如何在子线程中操作数据库,还请各位不吝赐教,谢谢。

解决方案 »

  1.   

    好像只有继承自ACTIVITY的类才支持managedQuery函数接口。
      

  2.   

    http://webcache.googleusercontent.com/search?hl=zh-CN&newwindow=1&safe=strict&rlz=1R2AMSB_zh-CNCN407&q=cache:yRu1d-uPTh4J:http://mail.org/message/6ppxrptprbifroqm+service%E8%B0%83%E7%94%A8managedQuery&ct=clnk
    com.googlegroups.android-developers You can just use ContentResolver.query() directly. managedQuery() is a simple wrapper on top of that, which also takes care of managing the cursor in conjunction with the activity's lifecycle. The lifecycle of a service is much simpler (and open-ended) so it doesn't make sense to have a managedQuery() method on it. On Jan 6, 3:20 pm, "[email protected]" <[email protected]> wrote: Hello How can i create a cursor from managedQuery(returns a cursor from a content provider, and updates automatically data from it), in a service. I need to access a database from a service, and insert some data, but before i have to verify that my data is correct. Or maybe there is something else, but and i don't see how. Please help. 
      

  3.   

    后台Service当然可以操作数据库,Activity中有managedQuery而Service中没有的原因可能是Activity中用的比较多,而Service中用的比较少,所以就不提供了。因为设计一个接口需要考虑它是不是经常被用到,如果人人都提出一个需求,要求增加接口,那肯定会膨胀的。