Search code examples
androidbroadcastreceiversharedpreferencesandroid-2.2-froyo

getSharedPreferences(sharedprefname, Context.MODE_PRIVATE) in a broad cast receiver returns null pointer exception?



I'm developing an android application.I'm using Android SDK 2.2
I'm using 2 Broadcast receivers. Receiver A start a service S1
Receiver B starts a service S2.
Both the receivers invoked at a time interval of 1 hour
Receiver A is invoked @ a time 9.00, 10.00, etc
Receiver B is invoked @ a time 8.50, 9.50, etc
In service S1 I'm storing some data in shared preferences
I'm storing some data in shared preferences in the receiver B.
I'm using the code below in the BroadcastReceiver for getting shared preference. But it throws null pointer exception

context.getApplicationContext().getSharedPreferences("myPrefs_capture_gps_per_hour", Context.MODE_PRIVATE);


in the code above context is the object of the class BusinessModel. This BusinessModel class is the child class of the Application(android.app.Application)


Solution


  • I got the solution.
    The following statement is used to get the shared preferences inside the broadcast receiver


    mContext.getSharedPreferences("myPrefs_capture_gps_per_hour", Context.MODE_PRIVATE);
    



    In the above code "mContext" is the context passed in the onReceive() method