When I was trying to access a SharedPreferences
value from my Android app’s SyncAdapter
, I kept on receiving an old value that was saved to the key.
Quick solution to inaccessible SharedPreferences from SyncAdapter
A quick solution (which may be a short-term one) I tried was to remove android:process=":sync"
from the following code:
1 2 3 4 |
<service android:name=".service.SyncService" android:exported="true" android:process=":sync"> |
But beware:
The attribute android:process=”:sync” tells the system to run the Service in a global shared process named sync. If you have multiple sync adapters in your app they can share this process, which reduces overhead.
My long-term solution to inaccessible SharedPreferences from SyncAdapter
1 2 3 4 5 6 |
public static SharedPreferences getDefaultSharedPreferencesMultiProcess( Context context) { return context.getSharedPreferences( context.getPackageName() + "_preferences", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); } |
Source:
SharedPreference committed in SyncAdapter not updated in Activity?