Java Code Examples for android.content.SharedPreferences#OnSharedPreferenceChangeListener

The following examples show how to use android.content.SharedPreferences#OnSharedPreferenceChangeListener . 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: SettingsActivity.java    From homeDash with Apache License 2.0 7 votes vote down vote up
private void bindBoolPreferenceToHomeDashService(final int preferenceId, final BoolPreferenceAction boolPreferenceAction) {
    final String preferenceKey = getString(preferenceId);
    if(!onSharedPreferenceChangeListeners.containsKey(preferenceKey)) {
        SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
                if (s.equals(preferenceKey)) {
                    boolean newValue = sharedPreferences.getBoolean(s, false);
                    if (mBound) {
                        boolPreferenceAction.action(newValue);
                    }
                }
                ;
            }
        };
        onSharedPreferenceChangeListeners.put(preferenceKey, onSharedPreferenceChangeListener);
        sharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        onSharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferences, getString(preferenceId));
    }
}
 
Example 2
Source File: FragmentSettings.java    From privatelocation with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //set title
    getActivity().setTitle("Settings");

    //background color
    view.setBackgroundColor(getResources().getColor(R.color.white));

    sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            if(key.equals("RANDOMIZE_LOCATION") && cbRandomize.isChecked()) {
                Toast.makeText(context, getString(R.string.settings_randomize_toast), Toast.LENGTH_LONG).show();
            }
        }
    };
    sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
 
Example 3
Source File: MainActivity.java    From azure-notificationhubs-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    onSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (key == "registrationID") {
                String value = sharedPreferences.getString(key, "");
                updateRegistrationID(value);
            } else if (key == "FCMtoken") {
                String value = sharedPreferences.getString(key, "");
                updatePushToken(value);
            }
        }
    };

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);

    updateRegistrationID(sharedPreferences.getString("registrationID", ""));
    updatePushToken(sharedPreferences.getString("FCMtoken", ""));

    mainActivity = this;
    NotificationHelper.createChannelAndHandleNotifications(getApplicationContext());
}
 
Example 4
Source File: BoardActivity.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
    addPreferencesFromResource(R.xml.board_preferences);

    mBoardTitlePreference = (EditTextPreference) findPreference(BoardUtils.BOARD_TITLE_KEY);

    setBoardTitlePreference();

    mListenerOptions = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

            switch (key) {
                case BoardUtils.BOARD_TITLE_KEY:
                    updateBoardTitlePreferenceSummary();
                    break;
                case BoardUtils.CUSTOM_APP_KEY:
                    manageCustomAppPreference();
                    break;
            }
        }
    };

    mCustomAppPreference = findPreference(BoardUtils.CUSTOM_APP_PREF_KEY);
    manageCustomAppPreference();
    mCustomAppPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Intent intent = new Intent(getActivity(), MultiSelectRecyclerViewActivity.class);
            getActivity().startActivity(intent);
            return false;
        }
    });
}
 
Example 5
Source File: SecurePreferences.java    From secure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Registers SecureStorageChangeListener to listen to any changes in SecureStorage
 *
 * @param context  Context is used internally
 * @param listener Provided listener with given behaviour from the developer that will be registered
 */
public static void registerOnSharedPreferenceChangeListener(@NonNull Context context,
                                                            @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener) {
    Context applicationContext = context.getApplicationContext();
    SharedPreferences preferences = applicationContext
            .getSharedPreferences(KEY_SHARED_PREFERENCES_NAME, MODE_PRIVATE);
    preferences.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 6
Source File: SecurePreferences.java    From secure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Unregisters SecureStorageChangeListener from SecureStorage
 *
 * @param context  Context is used internally
 * @param listener Provided listener with given behaviour from the developer that will be unregistered
 */
public static void unregisterOnSharedPreferenceChangeListener(@NonNull Context context,
                                                              @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener) {
    Context applicationContext = context.getApplicationContext();
    SharedPreferences preferences = applicationContext
            .getSharedPreferences(KEY_SHARED_PREFERENCES_NAME, MODE_PRIVATE);
    preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
 
Example 7
Source File: AbstractPreference.java    From Common with Apache License 2.0 4 votes vote down vote up
public void registerOnSharePreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mSettings.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 8
Source File: Pref.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    synchronized (this) {
        mListeners.put(listener, mContent);
    }
}
 
Example 9
Source File: BasePreferences.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mPreferences.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 10
Source File: TinyDB.java    From IdeaTrackerPlus with MIT License 4 votes vote down vote up
/**
 * Unregister SharedPreferences change listener
 *
 * @param listener listener object of OnSharedPreferenceChangeListener to be unregistered
 */
public void unregisterOnSharedPreferenceChangeListener(
        SharedPreferences.OnSharedPreferenceChangeListener listener) {

    preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
 
Example 11
Source File: UserUtil.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setOnAccountKeeperChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mPreferences.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 12
Source File: Settings.java    From andOTP with MIT License 4 votes vote down vote up
public void registerPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    settings.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 13
Source File: CalculatorSetting.java    From ncalc with GNU General Public License v3.0 4 votes vote down vote up
public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener
                                                               onSharedPreferenceChangeListener) {
    sharedPreferences.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
}
 
Example 14
Source File: SharedPreferencesPropertyBackend.java    From memetastic with GNU General Public License v3.0 4 votes vote down vote up
public void registerPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener value) {
    registerPreferenceChangedListener(_prefApp, value);
}
 
Example 15
Source File: SharedPreferencesPropertyBackend.java    From memetastic with GNU General Public License v3.0 4 votes vote down vote up
public void unregisterPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener value) {
    unregisterPreferenceChangedListener(_prefApp, value);
}
 
Example 16
Source File: AbstractPreference.java    From DMusic with Apache License 2.0 4 votes vote down vote up
public void unregisterOnSharePreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mSettings.unregisterOnSharedPreferenceChangeListener(listener);
}
 
Example 17
Source File: PreferenceUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public void unregisterOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) {
    mPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
 
Example 18
Source File: SharedPreferencesUtil.java    From android-ponewheel with MIT License 4 votes vote down vote up
public void registerListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    androidSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
 
Example 19
Source File: SharedPreferencesUtil.java    From android-ponewheel with MIT License 4 votes vote down vote up
public void removeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    androidSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
}
 
Example 20
Source File: SettingsActivity.java    From Gallery-example with GNU General Public License v3.0 2 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.prefs);

    PreferenceScreen preferenceScreen = getPreferenceScreen();

    Preference hiddenFolders = findPreference("hiddenFolders");

    SQLiteDatabase hiddenFoldersDB = getActivity().openOrCreateDatabase("HIDDEN", MODE_PRIVATE, null);

    final ArrayList<String> mHiddenFolders = new ArrayList<>();

    Cursor cursor = hiddenFoldersDB.rawQuery("SELECT * FROM foldersList;", null);

    if (cursor != null && cursor.moveToFirst()) {

        while (!cursor.isAfterLast()) {

            mHiddenFolders.add(cursor.getString(cursor.getColumnIndex("folder")));
            cursor.moveToNext();
        }
        cursor.close();
    }


    if (mHiddenFolders.size() != 0) {

        preferenceScreen.addPreference(hiddenFolders);

        hiddenFolders.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {

                HiddenFoldersDialog.show(getActivity(), mHiddenFolders);

                return false;
            }
        });
    } else {
        preferenceScreen.removePreference(hiddenFolders);
    }


    mListenerOptions = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences preftheme, String key) {

            if (key.equals(getResources().getString(R.string.pref_theme)) | key.equals(getResources().getString(R.string.pref_grid))) {

                restartApp(getActivity());
            }
        }
    };


}