Shared Preferences
Telefonda Shared Preference yap覺s覺 ile veri saklama

Dosyalar覺 Oluturma

private String sharedPrefFile = "com.example.android.hellosharedprefs";
mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);

Dosyalar覺 Kaydetme

@Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putInt("count", mCount);
preferencesEditor.putInt("color", mCurrentColor);
preferencesEditor.apply();
}

Dosyalar覺 Geri Alma

mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
if (savedInstanceState != null) {
mCount = mPreferences.getInt("count", 1);
mShowCount.setText(String.format("%s", mCount));
mCurrentColor = mPreferences.getInt("color", mCurrentColor);
mShowCount.setBackgroundColor(mCurrentColor);
} else { ... }

完 Dosyalar覺 Temizleme

SharedPreferences.Editor preferencesEditor = mPreferences.edit();
preferencesEditor.putInt("number", 42);
preferencesEditor.clear();
preferencesEditor.apply();

Deiikleri Takip Etme

public class SettingsActivity extends PreferenceActivity
implements OnSharedPreferenceChangeListener {
public static final String KEY_PREF_SYNC_CONN =
"pref_syncConnectionType";
// ...
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences,
String key) {
if (key.equals(KEY_PREF_SYNC_CONN)) {
Preference connectionPref = findPreference(key);
// Set summary to be the user-description for
// the selected value
connectionPref.setSummary(
sharedPreferences.getString(key, ""));
}
}
}
@Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(
SharedPreferences prefs, String key) {
// listener implementation
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);

儭 Eski Notlar

儭 Veri Oluturma ve Alma

  • val veri= this.getSharedPreferences(this.packageName, android.content.Context.MODE_PRIVATE) // Veri kayd覺n覺 deikene atama
    • this.packageName : paket ismi (com.... en 羹st sat覺rdaki)
    • MODE_PRIVATE : sadece benim uygulamamdan eriilebilirlik
  • var age1 = 30
  • veri.edit().putInt("userAge", age1).apply() // Veriyi kaydetme
    • userAge : anahtar
    • age1 : deer / deiken
  • val age2= veri.getInt("userAge", 0) // Kay覺tl覺 veriyi alma
    • userAge : anahtar (put'takini almak i癟in ayn覺 olmal覺)
    • 0 : Eer anahtar yoksa, varsay覺lan deer atamas覺
  • println("stored age : $storedAge") // veriyi g繹sterme

Veri G羹ncelleme

age = 31
veri.edit().putInt("userAge", age).apply() // Daha 繹nceden olan bir anahtar覺n 羹st羹ne kaydedilirse g羹ncelleme olur.

尬 Veri Silme

  • veri.edit().remove("userAge").apply() // Veri silindi
    • userAge : silinecek anahtar
  • val age3 = veri.getInt("userAge", 0) // Veri olmad覺覺 i癟in age3 = 0 olacak.
    • userAge : anahtar
    • 0 : varsay覺lan deer

Faydal覺 Balant覺lar