从广播接收器访问数据库?

时间:2011-07-29 18:40:44

标签: android database sqlite broadcastreceiver

我无法从SQlite访问我的broadcast receiver数据库。它甚至可能吗?我一直在NullPointerException。在onReceive of the BroadcastReceiver我打电话

mDbHelper = new ResponderDbAdapter(context);

mDbHelper.open();

这与上下文有关吗?我想将在MainActivity中设置的数据库信息提取到BroadCast Receiver

想法?

编辑:

ERROR:
07-29 16:43:46.491: ERROR/AndroidRuntime(6772): FATAL EXCEPTION: main
07-29 16:43:46.491: ERROR/AndroidRuntime(6772): java.lang.RuntimeException: Unable to start receiver com.test.example.feed.receiver.RecieveSMS: java.lang.NullPointerException
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.os.Looper.loop(Looper.java:130)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.app.ActivityThread.main(ActivityThread.java:3684)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at java.lang.reflect.Method.invoke(Method.java:507)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:845)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at dalvik.system.NativeStart.main(Native Method)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772): Caused by: java.lang.NullPointerException
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at com.test.example.feed.receiver.RecieveSMS.onReceive(RecieveSMS.java:67)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
07-29 16:43:46.491: ERROR/AndroidRuntime(6772):     ... 10 more

编辑:一个更好的解释

我有一个单独的活动,允许用户将项目添加到数据库。我想与接收器分享这一点,以便广播接收器知道数据库中有哪些项目。 ResponderDbAdapter是一个访问Db并对DB执行提取和插入的类。上下文是什么...如果我的广播接收器运行,即使应用程序已关闭,它如何访问数据库?

1 个答案:

答案 0 :(得分:0)

广播接收器版本的上下文可能不适用于DBHelper。让Receiver启动服务并让服务访问DB