Example usage for android.preference PreferenceManager getDefaultSharedPreferences

List of usage examples for android.preference PreferenceManager getDefaultSharedPreferences

Introduction

In this page you can find the example usage for android.preference PreferenceManager getDefaultSharedPreferences.

Prototype

public static SharedPreferences getDefaultSharedPreferences(Context context) 

Source Link

Document

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context.

Usage

From source file:Main.java

public static void markWelcomeDone(final Context context) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    sp.edit().putBoolean(PREF_WELCOME_DONE, true).apply();
}

From source file:Main.java

public static void setLastForumPageSaved(Context context, int forumPageNum) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor edit = prefs.edit();//www .  ja  v a2 s .  c o m
    edit.putInt(PREF_LAST_FORUM_PAGE_SAVED, forumPageNum);
    edit.commit();
}

From source file:Main.java

/**
 * Generates a device ID.//  w w  w .  ja v  a  2  s .c o m
 */
public synchronized static String getDeviceId(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String deviceId = preferences.getString("device_id", null);
    if (deviceId == null) {
        String chars = "123456789ABCDEFGHJKLMNPQRSTUVWXY";
        byte[] randomBytes = new byte[12];
        new SecureRandom().nextBytes(randomBytes);
        StringBuilder builder = new StringBuilder(12);
        for (int i = 0; i < 12; i++) {
            builder.append(chars.charAt((randomBytes[i] & 0xFF) % chars.length()));
        }
        deviceId = builder.toString();
        preferences.edit().putString("device_id", deviceId).apply();
    }
    return deviceId;
}

From source file:Main.java

private static SharedPreferences getPrefs(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context);
}

From source file:Main.java

public static Boolean isEnoughtBattery(Context context) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    Float acceptableBatteryLevel = Float.parseFloat(pref.getString("pref_battery_level", "40.0f"));
    Float batteryLevel = getBatteryLevel(context);

    return (batteryLevel >= acceptableBatteryLevel || isCharging(context));
}

From source file:Main.java

public static boolean needsMigration(Context context) {
    try {//w w  w  .  j  a  v  a 2 s  . c  o m
        int currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).versionCode;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

        return !preferences.contains("migration_" + currentVersion);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

private static void saveWeatherInfo(Context context, String tmp, String windDir, String windSc, String txt,
        String city, String publishTime, String cityCode) {
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean("city_selected", true);
    editor.putString("city_name", city);
    editor.putString("city_code", cityCode);
    editor.putString("tmp", tmp);
    editor.putString("wind_dir", windDir);
    editor.putString("wind_sc", windSc);
    editor.putString("weather_desp", txt);
    editor.putString("publish_time", publishTime);
    editor.apply();/*from   ww  w .  j a  v a 2s .  c  om*/
}

From source file:Main.java

public static boolean writeRecords(int gameId, String key, String value) {
    boolean result = false;
    try {/*from w w w.  j av  a2  s .c o  m*/
        SharedPreferences mPerferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        Editor editor = mPerferences.edit();
        editor.putString(key, value);
        editor.commit();
        result = true;
    } catch (Exception e) {
        result = false;
    }
    return result;
}

From source file:Main.java

public static String getDeviceCaption(@NonNull Context argContext) {
    SharedPreferences argSharedPreferences = PreferenceManager.getDefaultSharedPreferences(argContext);
    return argSharedPreferences.getString(deviceNameKey, getDeviceID());
}

From source file:Main.java

/**
 * Sets the preference.//  w  w w.j  a v a  2 s  . c om
 *
 * @param key the key
 * @param value the value
 */
public static void setPreference(String key, Object value, Context c) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    // The SharedPreferences editor - must use commit() to submit changes
    SharedPreferences.Editor editor = preferences.edit();
    if (value instanceof Integer)
        editor.putInt(key, ((Integer) value).intValue());
    else if (value instanceof Long)
        editor.putLong(key, ((Long) value));
    else if (value instanceof String)
        editor.putString(key, (String) value);
    else if (value instanceof Boolean)
        editor.putBoolean(key, (Boolean) value);
    else if (value instanceof Float)
        editor.putFloat(key, (Float) value);
    editor.commit();
    Log.e("Preference set: ", key + " => " + value);
}