SharedPreferences简单使用 String类型数据保存和获取
2016-06-05 20:51
351 查看
package com.example.denglujiemian.zhaoi.Utils;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferencesUtils {
private static final String path ="urse";
// 保存用户的密码
public static void setPasswd(Context context,String key,String Value) {
SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, Value);
editor.commit();
}
// 获取用户的密码
public static String getPasswd(Context context,String key) {
SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);
return sp.getString(key, null);
}
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPreferencesUtils {
private static final String path ="urse";
// 保存用户的密码
public static void setPasswd(Context context,String key,String Value) {
SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(key, Value);
editor.commit();
}
// 获取用户的密码
public static String getPasswd(Context context,String key) {
SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);
return sp.getString(key, null);
}
}
使用方式
// 把数据保存到SharedPreferences里边
SharedPreferencesUtils.setPasswd(getApplicationContext(),"mPhoneCode", mPhoneCode);
SharedPreferencesUtils.setPasswd(getApplicationContext(),"mPassword", mPassword);
//获取数据
String user_id = SharedPreferencesUtils.getPasswd(mContext,"user_id");
String user_name = SharedPreferencesUtils.getPasswd(mContext,"user_name");
String user_header_img = SharedPreferencesUtils.getPasswd(mContext,"user_header_img");
相关文章推荐
- 使用JDBC4.0操作XML类型的字段(保存获取xml数据)的方法
- 获取SQL Server 2005 中使用到的数据类型
- 使用NSUserDefaults保存简单的用户数据
- java读取excel的时候,使用getRichStringCellValue()读取到数值类型数据的时候就报错了
- 主键字段使用不同数据类型的简单比较
- redis 数据类型(一)string的使用
- redis 数据类型(一)string的使用
- 使用反射在泛型为Integer的ArrayList中添加String类型的数据
- cookies的简单使用 客户端保存临时数据和上传文件的就Query.uploadify(2.xx版本)的使用
- 使用arcpy获取简单的矢量数据信息
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- C++类中包含string类型数据使用read和write实现文件读取和输出时遇到的问题
- java获取redis中各种数据类型key对应的value代码简单封装
- 简单的获取网络数据 测试使用
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- ASP.NET AJAX中 复杂数据类型使用简单实例
- VB数组排序模块,使用的是快速排序法,支持 Variant、Double、Long、String……等多种数据类型数组排序。
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
- 使用C++ stringstream来进行数据类型转换