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 getPlusImageUrl(final Context context, final String accountName) {
    SharedPreferences sp = getSharedPreferences(context);
    return hasActiveAccount(context)
            ? sp.getString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_PLUS_IMAGE_URL), null)
            : null;//from w  ww  .j  a  va  2 s.  co m
}

From source file:com.swp.workshop.fbint.SessionStore.java

public static boolean restore(Facebook session, Context context) {
    SharedPreferences savedSession = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
    session.setAccessToken(savedSession.getString(TOKEN, null));
    session.setAccessExpires(savedSession.getLong(EXPIRES, 0));
    return session.isSessionValid();
}

From source file:Main.java

/**
 * @return the absolute path to the AnkiDroid directory.
 *///  w  ww .ja  v  a2  s. c  o  m
public static String getCurrentAnkiDroidDirectory(Context context) {
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(context.getApplicationContext());
    return preferences.getString("deckPath", getDefaultAnkiDroidDirectory());
}

From source file:Main.java

/**
 * Returning the stored values in the shared preference with values provided
 * as parameters/*from   w  w w  . j a va  2 s  .  c o m*/
 * 
 * @param context
 * @param key
 * @return
 */
public static final String getStringSharedPreference(Context context, String key) {
    if (context != null) {

        SharedPreferences Pref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
        String value = Pref.getString(key, "");
        return value;

    } else {
        return "";
    }
}

From source file:com.h6ah4i.android.compat.utils.SharedPreferencesJsonStringSetWrapperUtils.java

public static Set<String> getStringSet(SharedPreferences prefs, String key, Set<String> defaultReturnValue) {
    final String json = prefs.getString(key, null);
    final Set<String> values = new HashSet<String>();

    if (json != null) {
        try {//from   w ww  .  j  av a2s.  com
            final JSONArray a = new JSONArray(json);
            for (int i = 0; i < a.length(); i++) {
                final String value = a.optString(i);
                values.add(value);
            }
        } catch (JSONException e) {
            Log.e(TAG, "getStringSet", e);
        }
    }

    return values;
}

From source file:Main.java

/**
 * Gets an unique id for installation/*from  www  . j  ava  2 s.c  o m*/
 *
 * @return the unique id
 */
@Nullable
public static synchronized String getInstallationId(@NonNull Context context) {
    if (uniqueID == null) {
        SharedPreferences sharedPrefs = context.getSharedPreferences(PREF_UNIQUE_ID, Context.MODE_PRIVATE);
        uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);

        if (uniqueID == null) {
            uniqueID = UUID.randomUUID().toString();
            SharedPreferences.Editor editor = sharedPrefs.edit();
            editor.putString(PREF_UNIQUE_ID, uniqueID);
            editor.apply();
        }
    }

    return uniqueID;
}

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  ww. j  ava  2s . c  om

    return null;
}

From source file:Main.java

/**
 * Gets a string value from preferences//w ww.j a va2 s  .com
 *
 * @param context      the context
 * @param keyId        the key id
 * @param defaultValue default value
 * @return the stored string value
 */
public static String getString(Context context, @StringRes int keyId, String defaultValue) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getString(getKey(context, keyId), defaultValue);
}

From source file:Main.java

/**
 * Gets a string value from preferences/* w  w w .  j ava  2  s . c om*/
 *
 * @param context      the context
 * @param keyId        the key id
 * @param defaultValue default value
 * @return the stored string value
 */
public static String getString(Context context, String keyId, String defaultValue) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.getString(keyId, defaultValue);
}

From source file:Main.java

/** 
 * @Thach/*from   w ww  .  j  a va2s . co  m*/
 * @Description: on get data to SharedPref with given key 
 * @param context
 * @param key 
 * @param msg data will be save
 */
public static String onGetPref(Context context, String key) {
    SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(context);
    return pre.getString(key, null);
}