android 中共享变量SharedPreferences的使用
2015-07-21 16:37
519 查看
SharedPreferences 的使用具体请参考API:
Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.
Note: currently this class does not support use across multiple processes. This will be added later.
使用如下:
Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.
Note: currently this class does not support use across multiple processes. This will be added later.
使用如下:
public boolean saveData(String userName,String password) { boolean flag = false; // 1.获取共享参数 SharedPreferences sharedPreferences = context.getSharedPreferences("qjg", context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE); // 2.通过共享参数获取编辑器对象 Editor editor = sharedPreferences.edit(); // 3.通过编辑器对象放入相应的值 editor.putString("name",userName); editor.putString("password",password); // 4.提交数据 ,必须使用。否则,写不到文件中! flag = editor.commit(); return flag; }
相关文章推荐
- Android 组件系列之Activity的传值和回传值
- 启动、运行超快的Android模拟器 genymotion
- Android从服务端获取json解析显示在客户端上面
- Android动态加载jar、apk的实现
- android整理的一些基础知识
- Android招财进宝手势密码的实现
- 导入 Android v4 包看不到源码的解决办法
- Android 自定义View (一)
- Android SimpleAdapter 的list刷新问题。
- android开发之service详解
- android开发之service详解
- android开发之service详解
- android开发之service详解
- onInterceptTouchEvent()和onTouchEvent()
- Android 集成支付宝遇到 Unable to execute dex: Multiple dex files define
- 【Android】动态注册广播接收器
- 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException...
- 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException
- GitHub上史上最全的Android开源项目分类汇总
- Android Intent 其中一个分析