Example usage for android.content SharedPreferences getString

List of usage examples for android.content SharedPreferences getString

Introduction

In this page you can find the example usage for android.content SharedPreferences getString.

Prototype

@Nullable
String getString(String key, @Nullable String defValue);

Source Link

Document

Retrieve a String value from the preferences.

Usage

From source file:Main.java

public static String getStoredVar(Activity activity, String key) {
    SharedPreferences settings = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
    return settings.getString(key, "");
}

From source file:Main.java

public static String getPreApn(Context context) {
    try {/*from  w  w  w.  j a  v a 2 s  .  c o  m*/
        SharedPreferences sharedPreferences = context.getSharedPreferences("com.cmmobi.icuiniao.preapn",
                Activity.MODE_PRIVATE);
        return sharedPreferences.getString("preapn", "wifi");
    } catch (Exception e) {
    }
    return "wifi";
}

From source file:Main.java

/**
 * getUserAgent returns the current user agent.
 * @param context// ww w .  j a v a2s.  c  om
 * @return
 */
public static String getUserAgent(Context context) {

    if (context == null) {
        return "";
    }

    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
            Context.MODE_PRIVATE);
    return sharedPreferences.getString(SHARED_PREFERENCES_USER_AGENT, "");

}

From source file:Main.java

public static String getWhitelist(SharedPreferences prefs, String languageCode) {
    languageCode = "eng";
    if (languageCode.equals("eng")) {
        return prefs.getString(KEY_CHARACTER_WHITELIST_ENGLISH, getDefaultWhitelist(languageCode));
    } else {//from w w w  . ja  v a 2 s  .c om
        throw new IllegalArgumentException();
    }
}

From source file:Main.java

/**
 * 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.// w ww.ja  va2 s  .c om
 *
 * @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);
}

From source file:Main.java

public static boolean isCheckChangePermitted(Context context, Calendar date) {

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String limitActivePref = sharedPrefs.getString("active-date-key", "ALLOW_ALL");

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    Calendar yesterday;/*from   w  ww .  j a  v  a  2 s  .  co m*/

    switch (limitActivePref) {
    case "ALLOW_CURRENT":
        return (date.compareTo(today) == 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY_AND_WEEKEND":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, today.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY ? -2 : -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_ALL":
    default:
        return true;
    }
}

From source file:Main.java

public static String getBlacklist(SharedPreferences prefs, String languageCode) {
    languageCode = "eng";
    if (languageCode.equals("eng")) {
        return prefs.getString(KEY_CHARACTER_BLACKLIST_ENGLISH, getDefaultBlacklist(languageCode));
    } else {/*from   ww w  .ja  v a 2  s  .c  o m*/
        throw new IllegalArgumentException();
    }
}

From source file:Main.java

static void loadVARs(Context context) {
    SharedPreferences mPrefs = context.getSharedPreferences("usersettings", 0);
    String su = mPrefs.getString("SERVER_URL", "");
    setVAR("SERVER_URL", su);
    setVAR("NAME", mPrefs.getString("NAME", ""));
    setVAR("USERNAME", mPrefs.getString("USERNAME", ""));
    setVAR("ENC_KEY", mPrefs.getString("ENC_KEY", ""));
    setVAR("REG_ID", mPrefs.getString("REG_ID", ""));
    setVAR("VALID_SSL", mPrefs.getString("VALID_SSL", ""));
    setVAR("DEBUG", mPrefs.getString("DEBUG", ""));
    setVAR("TOKEN", mPrefs.getString("TOKEN", ""));
    setVAR("VERSION", mPrefs.getString("VERSION", "true"));
    setVAR("INTERVAL", mPrefs.getString("INTERVAL", "0"));
    setVAR("NETWORK_ONLY", mPrefs.getString("NETWORK_ONLY", "false"));
    setVAR("HIDE_ICON", mPrefs.getString("HIDE_ICON", "false"));
    Logd(TAG, "Loaded variables.");
}

From source file:Main.java

public static String fetchObjectSharePref(Context context, String key) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor prefsEditor = preferences.edit();
    return preferences.getString(key, "");
}

From source file:Main.java

public static Object get(Context context, String key, Object defaultObject) {
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    if (defaultObject instanceof String) {
        return sp.getString(key, (String) defaultObject);
    } else if (defaultObject instanceof Integer) {
        return sp.getInt(key, (Integer) defaultObject);
    } else if (defaultObject instanceof Boolean) {
        return sp.getBoolean(key, (Boolean) defaultObject);
    } else if (defaultObject instanceof Float) {
        return sp.getFloat(key, (Float) defaultObject);
    } else if (defaultObject instanceof Long) {
        return sp.getLong(key, (Long) defaultObject);
    }/*from w w w . ja v  a 2  s  . c  o  m*/
    return null;
}