Java tutorial
//package com.java2s; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; public class Main { /** * Sets the preference. * * @param key the key * @param value the value */ public static void setPreference(String key, Object value, Context c) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c); // The SharedPreferences editor - must use commit() to submit changes SharedPreferences.Editor editor = preferences.edit(); if (value instanceof Integer) editor.putInt(key, ((Integer) value).intValue()); else if (value instanceof Long) editor.putLong(key, ((Long) value)); else if (value instanceof String) editor.putString(key, (String) value); else if (value instanceof Boolean) editor.putBoolean(key, (Boolean) value); else if (value instanceof Float) editor.putFloat(key, (Float) value); editor.commit(); Log.e("Preference set: ", key + " => " + value); } }