package cn.darkal.networkdiagnosis.Utils; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; /** * Created by xuzhou on 2016/9/12. * SharedPreferenceUtils */ public class SharedPreferenceUtils { public static void putInt(Context context, String key, int value) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putInt(key, value).apply(); } catch (Exception e) { e.printStackTrace(); } } public static int getInt(Context context, String key, int defaultValue) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getInt(key, defaultValue); } catch (Exception e) { e.printStackTrace(); return defaultValue; } } public static void putLong(Context context, String key, long value) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putLong(key, value).apply(); } catch (Exception e) { e.printStackTrace(); } } public static long getLong(Context context, String key, long defaultValue) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getLong(key, defaultValue); } catch (Exception e) { e.printStackTrace(); return defaultValue; } } public static void putString(Context context, String key, String value) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putString(key, value).apply(); } catch (Exception e) { e.printStackTrace(); } } public static String getString(Context context, String key, String defaultValue) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getString(key, defaultValue); } catch (Exception e) { e.printStackTrace(); return defaultValue; } } public static void putBoolean(Context context, String key, boolean value) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().putBoolean(key, value).apply(); } catch (Exception e) { e.printStackTrace(); } } public static boolean getBoolean(Context context, String key, boolean defaultValue) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp.getBoolean(key, defaultValue); } catch (Exception e) { e.printStackTrace(); return defaultValue; } } public static void remove(Context context, String key) { try { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); sp.edit().remove(key).apply(); } catch (Exception e) { e.printStackTrace(); } } }