PreferenceUtils.java 2.66 KB
package com.ectrip.cyt.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Created by Administrator on 2016/2/17.
 */
public class PreferenceUtils {

    private static SharedPreferences mPreferences;
    private static String NAME = "MachineType";

    private static SharedPreferences getpreferences(Context context) {
        if (mPreferences == null) {
            mPreferences = context.getSharedPreferences(NAME, 0);
            return mPreferences;
        }
        return mPreferences;
    }

    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    public static boolean getBoolean(Context context, String key, boolean b) {
        SharedPreferences sp = getpreferences(context);

        return sp.getBoolean(key, b);
    }

    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = getpreferences(context);
        return sp.getString(key, defValue);
    }

    public static void putString(Context context, String key, String value) {
        SharedPreferences sp = getpreferences(context);
        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = getpreferences(context);
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }



    public static long getLong(Context context, String key) {
        return getLong(context, key, -1);
    }

    public static long getLong(Context context, String key, long defValue) {
        SharedPreferences sp = getpreferences(context);
        return sp.getLong(key, defValue);
    }

    public static void putLong(Context context, String key, long value) {
        SharedPreferences sp = getpreferences(context);
        Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.commit();
    }
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = getpreferences(context);
        return sp.getInt(key, defValue);
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences sp = getpreferences(context);
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }
}