Android数据存储sharedpreferences加密
2012-10-30 15:38:14 来源:WEB开发网核心提示:import java.util.List;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;/** * 存储配置信息的工具类 <br> * 注:可读取的数据类型有-<
import java.util.List; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; /** * 存储配置信息的工具类 <br> * 注:可读取的数据类型有-<code>boolean、int、float、long、String.</code> */ public class SharePreferenceUtil { private final String MAK = "innoview"; private final int MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE; private final SharedPreferences sharedpreferences; public SharePreferenceUtil(Context context, String fileName) { sharedpreferences = context.getSharedPreferences(fileName, MODE); } public boolean saveSharedPreferences(String key, String value) { SharedPreferences.Editor editor = sharedpreferences.edit(); try { editor.putString(key, AESEncryptor.encrypt(MAK, value)); } catch (Exception e) { editor.putString(key, value); e.printStackTrace(); } return editor.commit(); } public String loadStringSharedPreference(String key) { String str = null; try { str = sharedpreferences.getString(key, null); if (str != null && !"".equals(str)){ str = AESEncryptor.decrypt(MAK, str); } } catch (Exception e) { e.printStackTrace(); } return str; } public boolean saveSharedPreferences(String key, int value) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putInt(key, value); return editor.commit(); } public int loadIntSharedPreference(String key) { return sharedpreferences.getInt(key, 0); } public boolean saveSharedPreferences(String key, float value) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putFloat(key, value); return editor.commit(); } public float loadFloatSharedPreference(String key) { return sharedpreferences.getFloat(key, 0f); } public boolean saveSharedPreferences(String key, Long value) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putLong(key, value); return editor.commit(); } public long loadLongSharedPreference(String key) { return sharedpreferences.getLong(key, 0l); } public boolean saveSharedPreferences(String key, Boolean value) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(key, value); return editor.commit(); } public boolean loadBooleanSharedPreference(String key) { return sharedpreferences.getBoolean(key, false); } public boolean saveAllSharePreference(String keyName, List<?> list) { int size = list.size(); if (size < 1) { return false; } SharedPreferences.Editor editor = sharedpreferences.edit(); if (list.get(0) instanceof String) { for (int i = 0; i < size; i++) { editor.putString(keyName + i, (String) list.get(i)); } } else if (list.get(0) instanceof Long) { for (int i = 0; i < size; i++) { editor.putLong(keyName + i, (Long) list.get(i)); } } else if (list.get(0) instanceof Float) { for (int i = 0; i < size; i++) { editor.putFloat(keyName + i, (Float) list.get(i)); } } else if (list.get(0) instanceof Integer) { for (int i = 0; i < size; i++) { editor.putLong(keyName + i, (Integer) list.get(i)); } } else if (list.get(0) instanceof Boolean) { for (int i = 0; i < size; i++) { editor.putBoolean(keyName + i, (Boolean) list.get(i)); } } return editor.commit(); } public Map<String, ?> loadAllSharePreference(String key) { return sharedpreferences.getAll(); } public boolean removeKey(String key) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.remove(key); return editor.commit(); } public boolean removeAllKey() { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.clear(); return editor.commit(); } }
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接