Access a database from Broadcast receiver?
I cant access my SQlite
database from my broadcast receiver
. Is it even possible? I keep getting a NullPointerException
. In the onReceive of the BroadcastReceiver
I call
mDbHelper = new ResponderDbAdapter(context);
mDbHelper.open();
Is it something to do with the context? I want to pull database info that gets set in my MainActivity
into the BroadCast Receiver
.
Ideas?
EDIT :
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开发者_Go百科 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
EDIT: A Better Explanation
I have a separate activity that allows the user to add items to the database. I want to share this with the receiver so that the broadcast receiver knows what items are in the database. ResponderDbAdapter is a class that accesses the Db and performs fetches and inserts to the DB. What would the context be...if I have the broadcast receiver running even if the application is closed how can it access a database?
The Broadcast Receivers version of the context is probably not working for the DBHelper. Have the Receiver fire up a service and have the service access the DB
精彩评论