spUtil工具類的封裝
阿新 • • 發佈:2019-01-02
工具類的作用是為了讓我們更方便的使用,一次封裝,多次使用,也是一個合格的工程師必備的程式碼技術,下面就是sp存值和取值的工具類封裝
public class SpUtil { private static final String name="login"; private static final int sp_name=Context.MODE_PRIVATE; private static Context mContext=MyApp.context; public static boolean put(String key,Object value){ SharedPreferences sharedPreferences=mContext.getSharedPreferences(name,sp_name); SharedPreferences.Editor edit=sharedPreferences.edit(); if(value instanceof String){ if(!TextUtils.isEmpty((CharSequence) value)){ edit.putString(key, (String) value); } } else if(value instanceof Boolean){ edit.putBoolean(key, (Boolean) value); } else if(value instanceof Float){ edit.putFloat(key, (Float) value); } else if(value instanceof Float){ edit.putFloat(key, (Float) value); }else if(value instanceof Integer){ edit.putInt(key, (Integer) value); }else{ edit.putLong(key, (Long) value); } boolean commit = edit.commit(); return commit; } public static String getString(String key,String array){ SharedPreferences sharedPreferences = mContext.getSharedPreferences(name, sp_name); return sharedPreferences.getString(key,array); } public static int getInt(String key,int defualt){ SharedPreferences preferences=mContext.getSharedPreferences(name,sp_name); return preferences.getInt(key,defualt); } public static boolean getSb(String key,boolean defualt){ SharedPreferences preferences=mContext.getSharedPreferences(name,sp_name); return preferences.getBoolean(key,defualt); } public static long getLong(String key,long defualt){ SharedPreferences preferences=mContext.getSharedPreferences(name,sp_name); return preferences.getLong(key,defualt); } //摧毀方法 public static void remove(String key){ SharedPreferences sharedPreferences=mContext.getSharedPreferences(name,sp_name); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.remove(key); editor.commit(); } }