/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = SharedPreferences.GetLong(key, Convert.ToInt64(defaultValue)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue)); break; case TypeCode.UInt16: value = SharedPreferences.GetInt(key, Convert.ToUInt16(defaultValue)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.Double: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; } return((null != value) ? (T)value : defaultValue); } }
public bool IsAppUpdated() { int ver = GetVersionCode(); if (ver > SharedPreferences.GetInt("version", 0)) { SharedPreferences.Edit().PutInt("version", ver).Commit(); return(true); } else { return(false); } }
// =========================================================== // Constructors // =========================================================== public void OnCreate(/* final */ Bundle savedInstanceState) { base.OnCreate(savedInstanceState); this.SetContentView(R.Layout.list_examples); this.mExpandableExampleLauncherListAdapter = new ExpandableExampleLauncherListAdapter(this); this.SetListAdapter(this.mExpandableExampleLauncherListAdapter); /* TODO: Consider re-adding this if we * this.findViewById(R.id.btn_get_involved).setOnClickListener(new OnClickListener() { * @Override * public void onClick(final View pView) { * ExampleLauncher.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.andengine.org"))); * } * }); */ /* final */ SharedPreferences prefs = this.GetPreferences(FileCreationMode.Private); this.mVersionCodeCurrent = this.GetVersionCode(); this.mVersionCodeLastLaunch = prefs.GetInt(PREF_LAST_APP_LAUNCH_VERSIONCODE_ID, -1); if (this.IsFirstTime("first.app.launch")) { this.ShowDialog(DIALOG_FIRST_APP_LAUNCH); } else if (this.mVersionCodeLastLaunch != -1 && this.mVersionCodeLastLaunch < this.mVersionCodeCurrent) { this.ShowDialog(DIALOG_NEW_IN_THIS_VERSION); } else if (IsFirstTime("please.submit.benchmarks")) { this.ShowDialog(DIALOG_BENCHMARKS_SUBMIT_PLEASE); } prefs.Edit().PutInt(PREF_LAST_APP_LAUNCH_VERSIONCODE_ID, this.mVersionCodeCurrent).Commit(); }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); bool resave = false; switch (typeCode) { case TypeCode.Decimal: //Android doesn't have decimal in shared prefs so get string and convert var savedDecimal = string.Empty; try { savedDecimal = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDecimal = SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove("key"); resave = true; } if (string.IsNullOrWhiteSpace(savedDecimal)) { value = Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDecimal(savedDecimal, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: //Android doesn't have double, so must get as string and parse. var savedDouble = string.Empty; try { savedDouble = SharedPreferences.GetString(key, string.Empty); } catch (Java.Lang.ClassCastException cce) { Console.WriteLine("Settings 1.5 change, have to remove key."); try { Console.WriteLine("Attempting to get old value."); savedDouble = SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)).ToString(); Console.WriteLine("Old value has been parsed and will be updated and saved."); } catch (Java.Lang.ClassCastException cce2) { Console.WriteLine("Could not parse old value, will be lost."); } Remove(key); resave = true; } if (string.IsNullOrWhiteSpace(savedDouble)) { value = Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture); } else { value = Convert.ToDouble(savedDouble, System.Globalization.CultureInfo.InvariantCulture); } if (resave) { AddOrUpdateValue(key, value); } break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }
/// <summary> /// Gets the current value or the default that you specify. /// </summary> /// <typeparam name="T">Vaue of t (bool, int, float, long, string)</typeparam> /// <param name="key">Key for settings</param> /// <param name="defaultValue">default value if not set</param> /// <returns>Value or default</returns> public T GetValueOrDefault <T>(string key, T defaultValue = default(T)) { lock (locker) { Type typeOf = typeof(T); if (typeOf.IsGenericType && typeOf.GetGenericTypeDefinition() == typeof(Nullable <>)) { typeOf = Nullable.GetUnderlyingType(typeOf); } object value = null; var typeCode = Type.GetTypeCode(typeOf); switch (typeCode) { case TypeCode.Decimal: value = (decimal)SharedPreferences.GetLong(key, (long)Convert.ToDecimal(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Boolean: value = SharedPreferences.GetBoolean(key, Convert.ToBoolean(defaultValue)); break; case TypeCode.Int64: value = (Int64)SharedPreferences.GetLong(key, (long)Convert.ToInt64(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.String: value = SharedPreferences.GetString(key, Convert.ToString(defaultValue)); break; case TypeCode.Double: value = (double)SharedPreferences.GetLong(key, (long)Convert.ToDouble(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Int32: value = SharedPreferences.GetInt(key, Convert.ToInt32(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.Single: value = SharedPreferences.GetFloat(key, Convert.ToSingle(defaultValue, System.Globalization.CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: var ticks = SharedPreferences.GetLong(key, -1); if (ticks == -1) { value = defaultValue; } else { value = new DateTime(ticks); } break; default: if (defaultValue is Guid) { var outGuid = Guid.Empty; Guid.TryParse(SharedPreferences.GetString(key, Guid.Empty.ToString()), out outGuid); value = outGuid; } else { throw new ArgumentException(string.Format("Value of type {0} is not supported.", value.GetType().Name)); } break; } return(null != value ? (T)value : defaultValue); } }
public static int GetInt(string key, int defaultValue) { return(SharedPreferences.GetInt(key, defaultValue)); }