-1

Problems using SharedPreferences on a Service (getPreferences doesn't exist on a service)

This topic didn't helped me, I created keys and saved them in an activity and tried to get them from the IntentService and I couldn't get them, I tried several ways.

Community
  • 1
  • 1
LamaTo
  • 540
  • 1
  • 8
  • 21
  • 1
    "I couldn't get them, I tried several ways" -- then please provide a [mcve] demonstrating the "several ways" that you tried and explaining what "couldn't get them" means for each. – CommonsWare Aug 31 '16 at 15:46

3 Answers3

1

I am accessing preferences from a Service in one of my apps, here is how I do it:

preferences = getApplicationContext().getSharedPreferences(
            "settings", MODE_PRIVATE);

Instead of settings you can write something else as well. Should be the same value that you use to access inside the activity.

Javanshir
  • 772
  • 2
  • 7
  • 20
  • thanks , actually I think the problem was that I didn't initiate the variables I sent as defaults, and it made some kind of inner problem that returned null for string's and false for boolean's. not sure about it but this is my conclusion. – LamaTo Aug 31 '16 at 15:18
0

Just use PreferenceManager.getDefaultSharedPreferences(context).getString(MY_PREFERENCE, null); from your service. It should work.

Amilcar Andrade
  • 1,131
  • 9
  • 16
0

Try this..

SharedPreferences sharedpreferences = this.getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
noufalcep
  • 3,446
  • 15
  • 33
  • 51
ssk360
  • 11
  • 1