I seem to be having trouble reading preferences from my AppWidgetProvider class. My code works in an Activity, but it does not in an AppWidgetProvider. Here is the code I am using to read back a boolean:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean autoreplyon = settings.getBoolean("autoreplyon", false);
However, I get the “The method getSharedPreferences(String, int) is undefined for the type widget” error (widget is the name of my AppWidgetProvider class).
Thanks in advance for any suggestions!
getSharedPreferences(), should you choose to use it, is only available on subclasses ofContext, likeActivityorService.AppWidgetProvideris a subclass ofBroadcastReceiver, which is not aContext.That being said, if you are going to use the
PreferenceScreensystem, or if you are not certain that it’s gotta gotta gotta begetSharedPreferences(), I would usePreferenceManager.getDefaultSharedPreferences()instead. Those are theSharedPreferencesthatPreferenceScreen/PreferenceActivitywill use.