Java Code Examples for android.content.SharedPreferences#edit()

The following examples show how to use android.content.SharedPreferences#edit() . 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: GameState.java    From Learning-Java-by-Building-Android-Games-Second-Edition with MIT License 6 votes vote down vote up
GameState(GameStarter gs, Context context){
    // This initializes the gameStarter reference
    gameStarter = gs;

    // Get the current high score
    SharedPreferences prefs;
    prefs = context.getSharedPreferences("HiScore",
            Context.MODE_PRIVATE);

    // Initialize the mEditor ready
    mEditor = prefs.edit();

    // Load high score from a entry in the file
    //  labeled "hiscore"
    // if not available highscore set to zero 0
    mHighScore = prefs.getInt("hi_score", 0);
}
 
Example 2
Source File: SharedConfig.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public static void toogleRaiseToSpeak()
{
    raiseToSpeak = !raiseToSpeak;
    SharedPreferences preferences = MessagesController.getGlobalMainSettings();
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean("raise_to_speak", raiseToSpeak);
    editor.commit();
}
 
Example 3
Source File: SettingsActivity.java    From Paddle-Lite-Demo with Apache License 2.0 5 votes vote down vote up
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(getString(R.string.CHOOSE_PRE_INSTALLED_MODEL_KEY))) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(getString(R.string.ENABLE_CUSTOM_SETTINGS_KEY), false);
        editor.commit();
    }
    reloadPreferenceAndUpdateUI();
}
 
Example 4
Source File: Fido2DemoActivity.java    From security-samples with Apache License 2.0 5 votes vote down vote up
private void clearAccountSignInStatus() {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean(Constants.PREF_SIGNED_IN_STATUS, false);
    Log.d(TAG, "Clear account sign in status");
    editor.apply();
}
 
Example 5
Source File: GameControllerActivity.java    From android-robocar with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    SharedPreferences sp = getSharedPreferences("ui-resources", 0);
    SharedPreferences.Editor spe = sp.edit();
    String baseUrl = sp.getString("webserviceUrl", "");
    if (baseUrl != null && !baseUrl.isEmpty())
        this.webserviceUrlTextView.setText(baseUrl);
}
 
Example 6
Source File: IntroActivity.java    From habpanelviewer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDonePressed(Fragment currentFragment) {
    super.onDonePressed(currentFragment);

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    if (!prefs.getBoolean(Constants.PREF_INTRO_SHOWN, false)) {
        SharedPreferences.Editor editor1 = prefs.edit();
        editor1.putBoolean(Constants.PREF_INTRO_SHOWN, true);
        editor1.apply();
    }
    finish();
}
 
Example 7
Source File: StartupReceiver.java    From your-local-weather with GNU General Public License v3.0 5 votes vote down vote up
private void removeOldPreferences(Context context) {
    SharedPreferences mSharedPreferences = context.getSharedPreferences(Constants.APP_SETTINGS_NAME,
            Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.remove(Constants.APP_SETTINGS_ADDRESS_FOUND);
    editor.remove(Constants.APP_SETTINGS_GEO_CITY);
    editor.remove(Constants.APP_SETTINGS_GEO_COUNTRY_NAME);
    editor.remove(Constants.APP_SETTINGS_GEO_DISTRICT_OF_COUNTRY);
    editor.remove(Constants.APP_SETTINGS_GEO_DISTRICT_OF_CITY);
    editor.remove(Constants.LAST_UPDATE_TIME_IN_MS);
    editor.remove(Constants.APP_SETTINGS_CITY);
    editor.remove(Constants.APP_SETTINGS_COUNTRY_CODE);
    editor.remove(Constants.WEATHER_DATA_WEATHER_ID);
    editor.remove(Constants.WEATHER_DATA_TEMPERATURE);
    editor.remove(Constants.WEATHER_DATA_DESCRIPTION);
    editor.remove(Constants.WEATHER_DATA_PRESSURE);
    editor.remove(Constants.WEATHER_DATA_HUMIDITY);
    editor.remove(Constants.WEATHER_DATA_WIND_SPEED);
    editor.remove(Constants.WEATHER_DATA_CLOUDS);
    editor.remove(Constants.WEATHER_DATA_ICON);
    editor.remove(Constants.WEATHER_DATA_SUNRISE);
    editor.remove(Constants.WEATHER_DATA_SUNSET);
    editor.remove(Constants.APP_SETTINGS_LATITUDE);
    editor.remove(Constants.APP_SETTINGS_LONGITUDE);
    editor.remove(Constants.LAST_FORECAST_UPDATE_TIME_IN_MS);
    editor.remove(Constants.KEY_PREF_UPDATE_DETAIL);
    editor.remove(Constants.APP_SETTINGS_UPDATE_SOURCE);
    editor.remove(Constants.APP_SETTINGS_LOCATION_ACCURACY);
    editor.remove(Constants.LAST_LOCATION_UPDATE_TIME_IN_MS);
    editor.remove(Constants.LAST_WEATHER_UPDATE_TIME_IN_MS);
    editor.remove(Constants.KEY_PREF_LOCATION_UPDATE_STRATEGY);
    editor.remove("daily_forecast");
    editor.commit();
    context.getSharedPreferences(Constants.PREF_WEATHER_NAME,
            Context.MODE_PRIVATE).edit().clear().commit();
}
 
Example 8
Source File: PreferencesActivity.java    From AnotherRSS with The Unlicense 5 votes vote down vote up
static public boolean storeArray(boolean[] array, String arrayName, Context mContext) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(arrayName +"_size", array.length);
    for(int i=0;i<array.length;i++)
        editor.putBoolean(arrayName + "_" + i, array[i]);
    return editor.commit();
}
 
Example 9
Source File: SharedUtil.java    From MegviiFacepp-Android-SDK with Apache License 2.0 5 votes vote down vote up
public void saveStringValue(String key, String value) {
	SharedPreferences sharePre = ctx.getSharedPreferences(FileName,
			Context.MODE_PRIVATE);
	SharedPreferences.Editor editor = sharePre.edit();
	editor.putString(key, value);
	editor.commit();
}
 
Example 10
Source File: Fido2DemoActivity.java    From android-fido with Apache License 2.0 5 votes vote down vote up
private void saveAccountSignInStatus() {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean(Constants.PREF_SIGNED_IN_STATUS, true);
    Log.d(TAG, "Save account sign in status: true");
    editor.apply();
}
 
Example 11
Source File: ProfileManager.java    From Cybernet-VPN with GNU General Public License v3.0 5 votes vote down vote up
public void saveProfileList(Context context) {
    SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
    Editor editor = sharedprefs.edit();
    editor.putStringSet("vpnlist", profiles.keySet());
    // For reasing I do not understand at all
    // Android saves my prefs file only one time
    // if I remove the debug code below :(
    int counter = sharedprefs.getInt("counter", 0);
    editor.putInt("counter", counter + 1);
    editor.apply();
}
 
Example 12
Source File: PreferenceUtilities.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
synchronized public static void incrementChargingReminderCount(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int chargingReminders = prefs.getInt(KEY_CHARGING_REMINDER_COUNT, DEFAULT_COUNT);

    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(KEY_CHARGING_REMINDER_COUNT, ++chargingReminders);
    editor.apply();
}
 
Example 13
Source File: SPUtils.java    From RxJava2RetrofitDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 移除某个key值已经对应的值
 */
public static void remove(Context context, String key) {
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.remove(key);
    SharedPreferencesCompat.apply(editor);
}
 
Example 14
Source File: PreferenceUtilities.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
synchronized private static void setWaterCount(Context context, int glassesOfWater) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(KEY_WATER_COUNT, glassesOfWater);
    editor.apply();
}
 
Example 15
Source File: MyMusicUtil.java    From MeetMusic with Apache License 2.0 4 votes vote down vote up
public static void setBingShared(String value){
    SharedPreferences pref = MyApplication.getContext().getSharedPreferences("bing_pic",MyApplication.getContext().MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("pic", value);
    editor.commit();
}
 
Example 16
Source File: SharedPreferenceUtils.java    From WanAndroid with MIT License 4 votes vote down vote up
public static boolean putData(String filename, String key, long value) {
    SharedPreferences sharedPreferences = WanAndroidApplication.getMyApplication().getSharedPreferences(filename, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putLong(key, value);
    return editor.commit();
}
 
Example 17
Source File: SunshinePreferences.java    From android-dev-challenge with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method to handle setting location details in Preferences (city name, latitude,
 * longitude)
 * <p>
 * When the location details are updated, the database should to be cleared.
 *
 * @param context  Context used to get the SharedPreferences
 * @param lat      the latitude of the city
 * @param lon      the longitude of the city
 */
public static void setLocationDetails(Context context, double lat, double lon) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sp.edit();

    editor.putLong(PREF_COORD_LAT, Double.doubleToRawLongBits(lat));
    editor.putLong(PREF_COORD_LONG, Double.doubleToRawLongBits(lon));
    editor.apply();
}
 
Example 18
Source File: PreferenceHelper.java    From java-n-IDE-for-Android with Apache License 2.0 3 votes vote down vote up
public static void setWidgetExistsPreference(Context context, int[] appWidgetIds) {

        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

        Editor editor = sharedPrefs.edit();

        for (int appWidgetId : appWidgetIds) {
            String widgetExists = WIDGET_EXISTS_PREFIX.concat(Integer.toString(appWidgetId));
            editor.putBoolean(widgetExists, true);
        }

        editor.commit();


    }
 
Example 19
Source File: SpUtil.java    From styT with Apache License 2.0 3 votes vote down vote up
/**
 * 保存long值
 *
 * @param context 上下文
 * @param key     键
 * @param value   值
 */
public static void putLong(Context context, String key, long value) {
    SharedPreferences sp = getSp(context);
    Editor editor = sp.edit();
    editor.putLong(key, value);
    editor.apply();
}
 
Example 20
Source File: SpUtil.java    From styT with Apache License 2.0 3 votes vote down vote up
/**
 * 保存int值
 *
 * @param context 上下文
 * @param key     键
 * @param value   值
 */
public static void putInt(Context context, String key, int value) {
    SharedPreferences sp = getSp(context);
    Editor editor = sp.edit();
    editor.putInt(key, value);
    editor.apply();
}