set Shared Preference - Android android.content

Android examples for android.content:SharedPreferences

Description

set Shared Preference

Demo Code

import android.content.Context;
import android.content.SharedPreferences;

public class Main {

  public static final String PREFS_NAME = "_java2s.com";

  public static void setSharedPreference(Context c, String preference, boolean prefValue) {

    SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean(preference, prefValue);
    editor.commit();//from   w w w .  j a  v a  2s  .c  o m
  }

  public static void setSharedPreference(Context c, String preference, String prefValue) {
    SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(preference, prefValue);
    editor.commit();
  }

}

Related Tutorials