Java tutorial
//package com.java2s; import android.content.Context; import android.content.SharedPreferences; public class Main { public static final String PREFERENCE_NAME = "settings_share"; /** * get string preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this * name that is not a string * @see #getString(Context, String, String) */ public static String getString(Context context, String key) { return getString(context, key, null); } /** * get string preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a string */ public static String getString(Context context, String key, String defaultValue) { SharedPreferences settings = getDefaultPreferences(context); return settings.getString(key, defaultValue); } private static SharedPreferences getDefaultPreferences(Context context) { return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); } }