Skip to content Skip to sidebar Skip to footer

Problems Using Sharedpreferences On A Service (getpreferences Doesn't Exist On A Service)

I have some shared preferences (latitude, longitude) that I want to access from a service, that is not subclassed from Activity. In particular, when I try to access getPreferences

Solution 1:

If you are only using one SharedPreferences for your application, have all your code get it via PreferenceManager.getDefaultSharedPreferences().

Solution 2:

Actually, most of you might be running into the problem, that on devices with API >=11, the shared preferences are not set for multi process use by default anymore.

Answer :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    prefs = getSharedPreferences(PREFS, 0 | Context.MODE_MULTI_PROCESS);
} else {
    prefs = getSharedPreferences(PREFS, 0);
}

Solution 3:

Little late to help you out on this, but I hope this helps someone in the future. Here is your problem:

publicvoidonCreate() {
       settings = this.getPreferences(MODE_WORLD_WRITEABLE);
       configEditor = settings.edit();
       writeSignalGPS();
    }

Since you only retrieve the Shared Preference file when the service is created, the file is never properly updated by the service and thus the data is never shared with the application. Before writing to Shared Preferences or retrieving any data that may have changed, make sure to retrieve the Shared Preference file (reload) again such as:

privateHandlerhandler=newHandler() {
    @OverridepublicvoidhandleMessage(Message msg) {
        if (currentLocation!=null) {
            settings = this.getPreferences(MODE_WORLD_WRITEABLE);
            configEditor = settings.edit();
            configEditor.putString("mylatitude", ""+currentLocation.getLatitude());
            configEditor.putString("mylongitude", ""+currentLocation.getLongitude());
            configEditor.commit();
        }
    }

then in your application:

     settings = this.getPreferences(MODE_WORLD_WRITEABLE);
     StringmyLat= setttings.getString("mylatitude","");

Also, anything on Android 3.0 that has a scenario where a service and an activity share Shared Prefs, you should use:

settings = this.getPreferences(MODE_MULTI_PROCESS);

Solution 4:

If you have declared SharedPreferences as:

privatestaticfinalStringPREFS_NAME="UserData"; 
privatestaticfinalStringPREFS_VALUE1="value1"; 

then use the below code to fetch values:

SharedPreferencespreferences= context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
value1 = preferences.getString(PREFS_VALUE1, "0");

In same way you can even save values to SharedPreferences.

Solution 5:

For people who bump into this ... I had a similar problem ... The true cause of the problem is we are trying to get/use a context that is not fully initialized .. I was able to use sharedPreferences normally outside of my constructor for my IntentService.

Post a Comment for "Problems Using Sharedpreferences On A Service (getpreferences Doesn't Exist On A Service)"