3

I have an .apk that has a Service and an Activity that I want to share preferences. I guess I use getSharedPreferences("somename", MODE_PRIVATE) to do that. A couple of questions though:

  1. Is MODE_PRIVATE OK? Does that mean it's private for the application or the Activity/Service?

  2. I have a PreferenceActivity. How do I tell it to manage "somename" instead of the default preferences?

Thanks!

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
LiteWait
  • 584
  • 4
  • 21
  • 42

1 Answers1

4

MODE_PRIVATE : File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID). So, MODE_PRIVATE is ok.

To share preferences between activity and service all you need is to use application context to access(read and write) the preferences, not activity or service context.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Application `Context` won't work unless both your `Activity` and `Service` are running on the same process. – Eido95 Apr 14 '17 at 14:14