1

I am fairly new to android.

I have an activity in my application. I used shared preferences to store some data related to that activity. Now, how can I access those stored data from another service class? Is it possible?

Your attention is highly appreciated.

Adeesha
  • 55
  • 2
  • 8

2 Answers2

5

Seeing as you're already in an application component that extends Context, you can simply use:

SharedPreferences prefs = this.getSharedPreferences("Pref name", Context.MODE_PRIVATE);

Make sure you call this in or after onCreate().

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1

Have a look here http://developer.android.com/guide/topics/data/data-storage.html#pref

You can only access SharedPreferences within the same application ID.

If you want to access it from another application's context, you could put the mode to world-readable (but stronly not recommended and depreciated in API 17).

ConcurrentHashMap
  • 4,998
  • 7
  • 44
  • 53