Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To access shared SharedPreference from service in android

Tags:

android

I want to access SharedPreference within my background service i'd used PreferenceManager.getDefaultSharedPreferences() but it gives nullpoint Exception

Thanks

like image 245
Prachi Avatar asked Sep 07 '11 12:09

Prachi


People also ask

How do I access SharedPreferences?

These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android. content. Context) with a context in the same package as this activity.

How can I see shared preferences in Android?

Open the device monitor by clicking it. Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.

Where is shared preference stored in Android?

SharedPreferences are stored in an xml file in the app data folder, i.e. SharedPreferences added during runtime are not stored in the Eclipse project. The default shared preferences file would actually be: /data/data/<package>/shared_prefs/<package>_preferences. xml .

What is shared preference explain with example?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.


1 Answers

You have to use the Context like this:

Context ctx = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
like image 145
Jack Avatar answered Sep 30 '22 16:09

Jack