androidのSharedPreferences存储集合对象
来源:互联网 发布:美乐网络官方 编辑:程序博客网 时间:2024/06/05 04:54
getSharedPreferences(String str, int MODE)
方法声明:
Public SharedPreferences getSharedPreferences(String str, int MODE)
参数说明:
mode:操作模式,有三种可能的选择:
MODE_PRIVATE:默认的操作,创建只能被调用应用程序访问的文件(或者是共享相同用户名的所有应用程序),常量值:0(0x00000000)
MODE_WORLD_READABLE:允许其他应用程序对创建的文件有读取的访问权限。常量值:1(0x00000001)
MODE_WORLD_WRITEABLE:允许其他的应用程序对创建的文件有写的访问权限。常量值:2(0x00000002)
返回值:返回一个能够用于获取或编辑偏好值的SharedPreferences实例对象。
问题整理:在更改拨号盘皮肤时,编辑遇到问题:
packages/apps/Contacts/src/com/android/contacts/dialpad/DialpadFragment.java:737:
无法从静态上下文中引用非静态 方法 getSharedPreferences(java.lang.String,int)
sharedpreference = Context.getSharedPreferences("dialmenu",.......
自己直接定义的:sharedpreference = Context.getSharedPreferences("dialmenu",.......
1.首先说 getSharedPreferences只能继承Activity才能使用
自己定义的类 class DialpadFragment extends DialMatchFragment ,不是Activity.
2. 无法从静态上下文中引用非静态 方法.
由于getSharedPreferences是依赖于上下文环境的,即为context,无论哪个类中,一定要通过activity类的context才能调用。
在类继承了activity中使用时。 声明一个 private Context context;
可以在oncreate()方法中,采用 context=this;
在类继承了Fragment中时,
可以在oncreate()方法中,采用 getActivity();
getActivity().getSharedPreferences("dialmenu",.......
***********************************************************************************************************************
接着看下,使用sharedpreference 如何存储集合对象的
public static String SceneList2String(HashMap<Integer, Boolean> hashmap)throws IOException {// 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// 然后将得到的字符数据装载到ObjectOutputStreamObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);// writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它objectOutputStream.writeObject(hashmap);// 最后,用Base64.encode将字节文件转换成Base64编码保存在String中String SceneListString = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));// 关闭objectOutputStreamobjectOutputStream.close();return SceneListString;}@SuppressWarnings("unchecked")public static HashMap<Integer, Boolean> String2SceneList(String SceneListString) throws StreamCorruptedException,IOException, ClassNotFoundException {byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),Base64.DEFAULT);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);HashMap<Integer, Boolean> SceneList = (HashMap<Integer, Boolean>) objectInputStream.readObject();objectInputStream.close();return SceneList;}public static boolean putHashMap(Context context, String key,HashMap<Integer, Boolean> hashmap) {SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit();try {String liststr = SceneList2String(hashmap);editor.putString(key, liststr);} catch (IOException e) {e.printStackTrace();}return editor.commit();}public static HashMap<Integer, Boolean> getHashMap(Context context,String key) {SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);String liststr = settings.getString(key, "");try {return String2SceneList(liststr);} catch (StreamCorruptedException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}
其他不做过多介绍,方便大家拿过来直接使用即可。
- androidのSharedPreferences存储集合对象
- android SharedPreferences 存储对象
- android SharedPreferences 存储对象
- android SharedPreferences 存储对象
- android SharedPreferences 存储对象
- Android SharedPreferences 存储复杂对象
- Android SharedPreferences 存储复杂对象
- Android SharedPreferences存储基本数据类型、List集合、Map集合、对象都在这了
- Android使用Gson和SharedPreferences存储对象
- android开发之SharedPreferences存储对象
- Android之SharedPreferences 存储复杂对象
- Android将对象存储到SharedPreferences中
- Android 中使用SharedPreferences存储通用对象
- Android将对象存储到SharedPreferences中
- SharedPreferences存储ArrayList集合
- 用SharedPreferences存储集合
- SharedPreferences 存储对象
- SharedPreferences 存储对象
- Google MapReduce中文版
- Unity3d面试题
- 用 gdb 调试 GCC 程序
- 算法竞赛入门经典 第2章 习题答案
- PF_INET AF_INET
- androidのSharedPreferences存储集合对象
- lisp 本质
- hdu3439 Lucas定理扩展
- Exchange Server 2013 运维系列——运行邮件删除命令(search-mailbox)的前提条件
- shape 使用参数说明
- Spring框架下的单元测试方法
- PHP中判断字符串是否含有中文
- 【Security】 查找本地 LocalSystem 的账户名称
- removeObjectIdenticalTo