下面列出了android.content.SharedPreferences#edit ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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();
}
/**
* 移除某个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);
}
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();
}
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();
}
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();
}
public static void toogleRaiseToSpeak()
{
raiseToSpeak = !raiseToSpeak;
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("raise_to_speak", raiseToSpeak);
editor.commit();
}
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();
}
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();
}
@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();
}
@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);
}
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();
}
@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();
}
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();
}
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();
}
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();
}
/**
* 保存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();
}
/**
* 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();
}
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();
}
/**
* 保存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();
}