Sto modificando e inserendo un SharedPreference nel mio SyncAdapter dopo una corretta sincronizzazione, ma non vedo il valore aggiornato quando accedo alla preferenza nella mia attività (piuttosto sto vedendo il vecchio valore). Che cosa sto facendo di sbagliato? Contesti diversi?SharedPreference commesso in SyncAdapter non aggiornato in Attività?
mio SyncAdapter dove posso aggiornare la preferenza:
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 and set the new participant id
PARTICIPANT_ID = newParticipantId;
prefs.edit().putString("participant_id", String.valueOf(newParticipantId)).commit();
}
}
}
Il Servizio inizializzazione del SyncAdapter con l'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) {
return sSyncAdapter.getSyncAdapterBinder();
}
}
Una funzione statica all'interno della applicazione chiamata dall'attività che controlla lo SharedPreference. Questo non restituisce il valore impegnato in SyncAdapter, ma il vecchio valore. (Il mio SettingsActivity e altre attività anche utilizzare il vecchio valore.):
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));
return false;
}
return true;
}
UPDATE: Mi è toccato il nuovo valore quando ho chiudere completamente l'applicazione (falla scorrere dal applicazioni in esecuzione). Ho anche un SharedPreferenceChangeListener, che non viene attivato quando la preferenza viene aggiornata.
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 to restart the loader.");
mRestartLoader = true;
}
}
};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// subscribe to the participant_id change lister
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
PARTICIPANT_ID = Integer.parseInt(prefs.getString("participant_id", "0"));
prefs.registerOnSharedPreferenceChangeListener(mParticipantIDPrefChangeListener);
}
Dal codice che hai postato sembra che non hai mai chiamare il 'startSync()' 'sul SyncAdapter' il che significa che il metodo' onPerformSync (...) 'non viene eseguita e il la preferenza non è mai cambiata. – Titus
@Titus Da qualche parte faccio e 'onPerformSync()' è definitivamente eseguito. Utilizza anche il nuovo valore nelle sincronizzazioni successive. Ho scoperto che 'SyncService' è in esecuzione in un processo separato (''). Ora sto cercando di capire come usare 'MODE_MULTI_PROCESS' con' PreferenceManager.getDefaultSharedPreferences() '. –
matsch
Puoi usare 'prefs = mContext.getSharedPreferences (" myAppPrefs ", Context.MODE_MULTI_PROCESS);' [Qui] (http://developer.android.com/reference/android/content/Context.html#getSharedPreferences (java.lang .String, int)) è la documentazione. – Titus