Code example for SharedPreferences

Methods: registerOnSharedPreferenceChangeListener

0
 
	public VibratorContainer(@Nullable Vibrator vibrator, @Nonnull SharedPreferences preferences, float vibrationTimeScale) {
		this.vibrator = vibrator;
		this.vibrationTimeScale = vibrationTimeScale;
 
		preferences.registerOnSharedPreferenceChangeListener(this);
		onSharedPreferenceChanged(preferences, null);
 
	} 
 
	public void vibrate() { 
		if (time > 0 && vibrator != null) {
			vibrator.vibrate(time);
		} 
	} 
 
	@Override 
	public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
		if ( Preferences.hapticFeedbackEnabled.getPreference(preferences)) {
			//noinspection ConstantConditions 
			this.time = getScaledValue(Preferences.hapticFeedbackDuration.getPreference(preferences));
		} else { 
			this.time = 0;
		} 
	}