Java Code Examples for android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged()

The following examples show how to use android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewAndUpdatePreferencesActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// Load the preferences from an XML resource
	addPreferencesFromResource(R.xml.user_prefs);

	// Get the username Preference
	mUserNamePreference = (Preference) getPreferenceManager()
			.findPreference(USERNAME);

	// Attach a listener to update summary when username changes
	mListener = new OnSharedPreferenceChangeListener() {
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			mUserNamePreference.setSummary(sharedPreferences.getString(
					USERNAME, "None Set"));
		}
	};

	// Get SharedPreferences object managed by the PreferenceManager for
	// this Fragment
	SharedPreferences prefs = getPreferenceManager()
			.getSharedPreferences();

	// Register a listener on the SharedPreferences object
	prefs.registerOnSharedPreferenceChangeListener(mListener);

	// Invoke callback manually to display the current username
	mListener.onSharedPreferenceChanged(prefs, USERNAME);

}
 
Example 2
Source File: ViewAndUpdatePreferencesActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// Load the preferences from an XML resource
	addPreferencesFromResource(R.xml.user_prefs);

	// Get the username Preference
	mUserNamePreference = getPreferenceManager()
			.findPreference(USERNAME);

	// Attach a listener to update summary when username changes
	mListener = new OnSharedPreferenceChangeListener() {
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			mUserNamePreference.setSummary(sharedPreferences.getString(
					USERNAME, "None Set"));
		}
	};

	// Get SharedPreferences object managed by the PreferenceManager for
	// this Fragment
	SharedPreferences prefs = getPreferenceManager()
			.getSharedPreferences();

	// Register a listener on the SharedPreferences object
	prefs.registerOnSharedPreferenceChangeListener(mListener);

	// Invoke callback manually to display the current username
	mListener.onSharedPreferenceChanged(prefs, USERNAME);

}
 
Example 3
Source File: ViewAndUpdatePreferencesActivity.java    From coursera-android with MIT License 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// Load the preferences from an XML resource
	addPreferencesFromResource(R.xml.user_prefs);

	// Get the username Preference
	mUserNamePreference = (Preference) getPreferenceManager()
			.findPreference(USERNAME);

	// Attach a listener to update summary when username changes
	mListener = new OnSharedPreferenceChangeListener() {
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			mUserNamePreference.setSummary(sharedPreferences.getString(
					USERNAME, "None Set"));
		}
	};

	// Get SharedPreferences object managed by the PreferenceManager for
	// this Fragment
	SharedPreferences prefs = getPreferenceManager()
			.getSharedPreferences();

	// Register a listener on the SharedPreferences object
	prefs.registerOnSharedPreferenceChangeListener(mListener);

	// Invoke callback manually to display the current username
	mListener.onSharedPreferenceChanged(prefs, USERNAME);

}
 
Example 4
Source File: CrossProcessSharedPreferences.java    From deagle with Apache License 2.0 5 votes vote down vote up
void notifyListeners(final String key) {
	final List<OnSharedPreferenceChangeListener> listeners;
	synchronized (this) {
		listeners = new ArrayList<>(mListeners.keySet());
	}
	for (final OnSharedPreferenceChangeListener listener : listeners) {
		Log.d(CrossProcessSharedPreferences.TAG, "Notify listener: " + listener);
		listener.onSharedPreferenceChanged(this, key);
	}
}
 
Example 5
Source File: BroadcastEventBridge.java    From binaryprefs with Apache License 2.0 4 votes vote down vote up
private void notifyListenersInternal(String key) {
    for (OnSharedPreferenceChangeListener listener : currentListeners) {
        listener.onSharedPreferenceChanged(null, key);
    }
}
 
Example 6
Source File: SimpleEventBridge.java    From binaryprefs with Apache License 2.0 4 votes vote down vote up
private void notifyListeners(String key) {
    for (OnSharedPreferenceChangeListener listener : listeners) {
        listener.onSharedPreferenceChanged(null, key);
    }
}
 
Example 7
Source File: TrackDataHubTest.java    From mytracks with Apache License 2.0 4 votes vote down vote up
/**
 * Tests preferences change.
 */
public void testPreferencesChange() throws Exception {

  // Register two listeners
  PreferencesUtils.setString(
      context, R.string.stats_rate_key, PreferencesUtils.STATS_RATE_DEFAULT);
  PreferencesUtils.setString(
      context, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT);
  PreferencesUtils.setInt(context, R.string.recording_gps_accuracy_key,
      PreferencesUtils.RECORDING_GPS_ACCURACY_DEFAULT);
  PreferencesUtils.setInt(context, R.string.recording_distance_interval_key,
      PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT);

  dataSource.registerOnSharedPreferenceChangeListener(capture(preferenceChangeListenerCapture));
  expect(trackDataListener1.onMetricUnitsChanged(true)).andReturn(false);
  expect(trackDataListener1.onReportSpeedChanged(true)).andReturn(false);
  expect(
      trackDataListener1.onRecordingGpsAccuracy(PreferencesUtils.RECORDING_GPS_ACCURACY_DEFAULT))
      .andReturn(false);
  expect(trackDataListener1.onRecordingDistanceIntervalChanged(
      PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT)).andReturn(false);
  expect(trackDataListener1.onMapTypeChanged(PreferencesUtils.MAP_TYPE_DEFAUlT)).andReturn(false);
  
  expect(trackDataListener2.onMetricUnitsChanged(true)).andReturn(false);
  expect(trackDataListener2.onReportSpeedChanged(true)).andReturn(false);
  expect(
      trackDataListener2.onRecordingGpsAccuracy(PreferencesUtils.RECORDING_GPS_ACCURACY_DEFAULT))
      .andReturn(false);
  expect(trackDataListener2.onRecordingDistanceIntervalChanged(
      PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT)).andReturn(false);
  expect(trackDataListener2.onMapTypeChanged(PreferencesUtils.MAP_TYPE_DEFAUlT)).andReturn(false);
  replay();

  trackDataHub.start();
  trackDataHub.registerTrackDataListener(
      trackDataListener1, EnumSet.of(TrackDataType.PREFERENCE));
  trackDataHub.registerTrackDataListener(
      trackDataListener2, EnumSet.of(TrackDataType.PREFERENCE));
  verifyAndReset();

  // Change report speed to false
  expect(trackDataListener1.onReportSpeedChanged(false)).andReturn(false);
  expect(trackDataListener2.onReportSpeedChanged(false)).andReturn(false);
  replay();

  PreferencesUtils.setString(
      context, R.string.stats_rate_key, context.getString(R.string.stats_rate_pace));
  OnSharedPreferenceChangeListener listener = preferenceChangeListenerCapture.getValue();
  listener.onSharedPreferenceChanged(
      sharedPreferences, PreferencesUtils.getKey(context, R.string.stats_rate_key));
  verifyAndReset();

  // Change metric units to false
  expect(trackDataListener1.onMetricUnitsChanged(false)).andReturn(false);
  expect(trackDataListener2.onMetricUnitsChanged(false)).andReturn(false);
  replay();

  String imperialUnits = context.getString(R.string.stats_units_imperial);
  PreferencesUtils.setString(context, R.string.stats_units_key, imperialUnits);
  listener.onSharedPreferenceChanged(
      sharedPreferences, PreferencesUtils.getKey(context, R.string.stats_units_key));
  verifyAndReset();
}