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 int preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this * name that is not a int * @see #getInt(Context, String, int) */ public static int getInt(Context context, String key) { return getInt(context, key, -1); } /** * get int 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 int */ public static int getInt(Context context, String key, int defaultValue) { SharedPreferences settings = getDefaultPreferences(context); return settings.getInt(key, defaultValue); } private static SharedPreferences getDefaultPreferences(Context context) { return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); } }