2

Given that a Service may be running even when the launching activity is destroyed,
and also that data is passed usually using the extra bundle along the intent,
I wonder if the Service is able to directly access the activity's preferences.

My guess is no, it cannot. But it would save me writing a chunk of code if it can do it.

4

2 回答 2

1

Assuming that You're asking about SharedPreferences. If so, then documentation is clear (by link above):

Note: currently this class does not support use across multiple processes. This will be added later.

So, currently, if Your service is running in a separate process it's not possible. In that case I would suggest to store Your preferences not in a SharedPreferences, but in some base which could be accessible via specific ContentProvider.

P.S. Actually I tried access SharedPreferences from multiple processes and it has worked (at least in my case), but I've decided to use another way because of the documentation mentioned in the answer.

于 2013-02-19T08:51:13.760 回答
0

If you mean SharedPreferences then you can just call getSharedPreferences with the same name.

于 2013-02-19T08:48:22.717 回答