user1933770
user1933770

Reputation: 11

using sharedpreferences from service

I'm trying to use sharedpreferences from my thread called by service:

SharedPreferences startPref = PreferenceManager.getDefaultSharedPreferences(context);
            //SharedPreferences startPref = context.getSharedPreferences("MyPref", 0);
            startPref.edit().putString("REFRESHED", when);
            startPref.edit().commit();
            System.out.println("Time put " + when);
            System.out.println("Got time " + startPref.getString("REFRESHED", "WRONG"));

I try to put there some string and then take it back, but I always get WRONG default message. I've tried to use both variants of startPref initializing: commented and not, both don't work.

context is service's Context.

Upvotes: 0

Views: 841

Answers (1)

laalto
laalto

Reputation: 152867

Change

startPref.edit().putString("REFRESHED", when);
startPref.edit().commit();

to

startPref.edit().putString("REFRESHED", when).commit();

Each call to edit() creates a new SharedPreferences.Editor instance. So, you are leaving your changes uncommitted in one and committing no changes in another editor.

Upvotes: 5

Related Questions