I am trying to access shared preferences from a service. I have used the the following to save the value of text to a string...
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Ignore1_value", Example.getText().toString());
editor.commit();
But, how would i get the value in a service? Everything i have tried returns as nothing. Any help would be perfect and much appreciated?
I looked through some other questions as well with no solution. I have came up with this, but like i said it returns it as no text.
Context ctx = getApplicationContext();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
String example1string = sharedPreferences.getString("Ignore1_value","");
Log.i("**GetSettings", example1string);
I'm always using PreferenceManager.getDefaultSharedPreferences(context)
. This is the same for all Context
s in your application.
A Service
is a Context
itself, so this would be sufficient:
PreferenceManager.getDefaultSharedPreferences(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With