Java tutorial
//package com.java2s; /* * Copyright (C) 2016 Pranav Pandey * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; public class Main { /** * Retrieve a boolean value from the preferences. * * @param context to retrieve Default SharedPreferences. * @param key The name of the preference to modify. * @param value The new value for the preference. * * @return Returns the preference value if it exists, or defValue. Throws * ClassCastException if there is a preference with this name that is not * a boolean. * * @throws ClassCastException * * @see {@link android.preference.PreferenceManager#getDefaultSharedPreferences(Context)}. */ public static boolean loadPrefs(Context context, String key, boolean value) { SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context); return getPrefs.getBoolean(key, value); } /** * Retrieve an integer value from the preferences. * * @param context to retrieve Default SharedPreferences. * @param key The name of the preference to retrieve. * @param value Value to return if this preference does not exist. * * @return Returns the preference value if it exists, or defValue. Throws * ClassCastException if there is a preference with this name that is not * a boolean. * * @throws ClassCastException * * @see {@link android.preference.PreferenceManager#getDefaultSharedPreferences(Context)}. */ public static int loadPrefs(Context context, String key, int value) { SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context); return getPrefs.getInt(key, value); } /** * Retrieve a String value from the preferences. * * @param context to retrieve Default SharedPreferences. * @param key Name of the preference to retrieve. * @param value Value to return if this preference does not exist. * * @return Returns the preference value if it exists, or defValue. Throws * ClassCastException if there is a preference with this name that is not * a boolean. * * @throws ClassCastException * * @see {@link android.preference.PreferenceManager#getDefaultSharedPreferences(Context)}. */ public static String loadPrefs(Context context, String key, String value) { SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context); return getPrefs.getString(key, value); } }