android - SharedPreference committed in SyncAdapter not updated in Activity? -
android - SharedPreference committed in SyncAdapter not updated in Activity? -
i changing , committing sharedpreference in syncadapter after successful sync, not seeing updated value when access preference in activity (rather seeing old value). doing wrong? different contexts?
my syncadapter update preference:
class syncadapter extends abstractthreadedsyncadapter { private int participant_id; private final context mcontext; private final contentresolver mcontentresolver; public syncadapter(context context, boolean autoinitialize) { super(context, autoinitialize); mcontext = context; mcontentresolver = context.getcontentresolver(); } public syncadapter(context context, boolean autoinitialize, boolean allowparallelsyncs) { super(context, autoinitialize, allowparallelsyncs); mcontext = context; mcontentresolver = context.getcontentresolver(); } @override public void onperformsync(account account, bundle extras, string authority, contentproviderclient provider, syncresult syncresult) { final sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(mcontext); participant_id = integer.parseint(prefs.getstring("participant_id", "0")); if (success) { // save , set new participant id participant_id = newparticipantid; prefs.edit().putstring("participant_id", string.valueof(newparticipantid)).commit(); } } } the service initializing syncadapter applicationcontext:
public class syncservice extends service { private static final object ssyncadapterlock = new object(); private static syncadapter ssyncadapter = null; @override public void oncreate() { synchronized (ssyncadapterlock) { if (ssyncadapter == null) { ssyncadapter = new syncadapter(getapplicationcontext(), false); } } } @override public ibinder onbind(intent intent) { homecoming ssyncadapter.getsyncadapterbinder(); } } a static function within application called activity checks sharedpreference. not homecoming value committed in syncadapter, old value. (my settingsactivity , other activities utilize old value.):
public static boolean isuserloggedin(context ctx) { final sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(ctx); int participantid = integer.parseint(prefs.getstring("participant_id", "0")); logd("dg_utils", "isuserloggedin.participantid: " + participantid);// todo if (participantid <= 0) { ctx.startactivity(new intent(ctx, loginactivity.class).addflags(intent.flag_activity_clear_top)); homecoming false; } homecoming true; } update: getting new value when close app (swipe apps running). have sharedpreferencechangelistener, not fired when preference updated.
private final sharedpreferences.onsharedpreferencechangelistener mparticipantidprefchangelistener = new sharedpreferences.onsharedpreferencechangelistener() { public void onsharedpreferencechanged(sharedpreferences prefs, string key) { if (key.equals("participant_id")) { logi(tag, "participant_id has changed, requesting restart loader."); mrestartloader = true; } } }; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // subscribe participant_id alter lister final sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(getactivity()); participant_id = integer.parseint(prefs.getstring("participant_id", "0")); prefs.registeronsharedpreferencechangelistener(mparticipantidprefchangelistener); }
ok, figured out myself @titus help , after research , pieced solution problem.
the reason why defaultsharedpreferences of same context not updated have specified syncservice run in own process in androidmanifest.xml (see below). hence, starting android 2.3, other process blocked accessing updated sharedpreferences file (see this answer , android docs on context.mode_multi_process).
<service android:name=".sync.syncservice" android:exported="true" android:process=":sync" tools:ignore="exportedservice" > <intent-filter> <action android:name="android.content.syncadapter" /> </intent-filter> <meta-data android:name="android.content.syncadapter" android:resource="@xml/syncadapter" /> </service> so had set mode_multi_process when accessing sharedpreferences both in syncadapter , in ui process of app. because i've used preferencemanager.getdefaultsharedpreferences(context) extensively throughout app wrote utility method , replaced calls of preferencemanager.getdefaultsharedpreferences(context) method (see below). default name of preferences file hardcoded , derived the android source code , this answer.
public static sharedpreferences getdefaultsharedpreferencesmultiprocess( context context) { homecoming context.getsharedpreferences( context.getpackagename() + "_preferences", context.mode_private | context.mode_multi_process); } android sharedpreferences android-syncadapter
Comments
Post a Comment