本节引言:
本节给大家介绍的是第二种存储用户数据的方式,使用SharedPreferences(保存用户偏好参数)保存数据, 当我们的应用想要保存用户的一些偏好参数,比如是否自动登陆,是否记住账号密码,是否在Wifi下才能 联网等相关信息,如果使用数据库的话,显得有点大材小用了!我们把上面这些配置信息称为用户的偏好 设置,就是用户偏好的设置,而这些配置信息通常是保存在特定的文件中!比如windows使用ini文件, 而J2SE中使用properties属性文件与xml文件来保存软件的配置信息;而在Android中我们通常使用 一个轻量级的存储类——SharedPreferences来保存用户偏好的参数!SharedPreferences也是使用xml文件, 然后类似于Map集合,使用键-值的形式来存储数据;我们只需要调用SharedPreferences的getXxx(name), 就可以根据键获得对应的值!使用起来很方便!
1.SharedPreferences使用示例:
使用流程图:

实现代码示例:
运行效果图:
流程是输入账号密码后点击登录,将信息保存到SharedPreference文件中, 然后重启app,看到数据已经显示在文本框中了

另外保存后,我们可以在File Expoler打开data/data/<包名>可以看到在shared_prefs目录下 生成了一个xml文件(因为N5没root,这里找了以前的效果图):

点击导出到桌面可以看到里面的内容:

代码实现:
布局文件activity_main.xml的编写:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户登陆" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="请输入用户名" />
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码" />
<EditText
android:id="@+id/editpasswd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
编写简单的SP工具类:SharedHelper.java:
/**
* Created by Jay on 2015/9/2 0002.
*/
public class SharedHelper {
private Context mContext;
public SharedHelper() {
}
public SharedHelper(Context mContext) {
this.mContext = mContext;
}
//定义一个保存数据的方法
public void save(String username, String passwd) {
SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username);
editor.putString("passwd", passwd);
editor.commit();
Toast.makeText(mContext, "信息已写入SharedPreference中", Toast.LENGTH_SHORT).show();
}
//定义一个读取SP文件的方法
public Map<String, String> read() {
Map<String, String> data = new HashMap<String, String>();
SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
data.put("username", sp.getString("username", ""));
data.put("passwd", sp.getString("passwd", ""));
return data;
}
}
最后是MainActivity.java实现相关逻辑:
public class MainActivity extends AppCompatActivity {
private EditText editname;
private EditText editpasswd;
private Button btnlogin;
private String strname;
private String strpasswd;
private SharedHelper sh;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
sh = new SharedHelper(mContext);
bindViews();
}
private void bindViews() {
editname = (EditText)findViewById(R.id.editname);
editpasswd = (EditText)findViewById(R.id.editpasswd);
btnlogin = (Button)findViewById(R.id.btnlogin);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strname = editname.getText().toString();
strpasswd = editpasswd.getText().toString();
sh.save(strname,strpasswd);
}
});
}
@Override
protected void onStart() {
super.onStart();
Map<String,String> data = sh.read();
editname.setText(data.get("username"));
editpasswd.setText(data.get("passwd"));
}
}
2.读取其他应用的SharedPreferences
核心: 获得其他app的Context,而这个Context代表访问该app的全局信息的接口,而决定应用的唯一标识 是应用的包名,所以我们可以通过应用包名获得对应app的Context 另外有一点要注意的是:其他应用的SP文件是否能被读写的前提就是SP文件是否指定了可读或者 可写的权限,我们上面创建的是MODE_PRIVATE的就不可以了~所以说你像读别人的SP里的数据, 很难,另外,一些关键的信息,比如密码保存到SP里,一般都是会做加密的,所以只能自己写自己玩~ 等下会讲下常用的MD5加密方法!
实现流程图:

代码示例:
运行效果图:

代码实现:
我们读取SP的操作放在MainActivity.java中完成,点击按钮后读取SP,并通过Toast显示出来:
public class MainActivity extends AppCompatActivity {
private Context othercontext;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnshow = (Button) findViewById(R.id.btnshow);
btnshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获得第一个应用的包名,从而获得对应的Context,需要对异常进行捕获
try {
othercontext = createPackageContext("com.jay.sharedpreferencedemo", Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//根据Context取得对应的SharedPreferences
sp = othercontext.getSharedPreferences("mysp", Context.MODE_WORLD_READABLE);
String name = sp.getString("username", "");
String passwd = sp.getString("passwd", "");
Toast.makeText(getApplicationContext(), "Demo1的SharedPreference存的\n用户名为:" + name + "\n密码为:" + passwd, Toast.LENGTH_SHORT).show();
}
});
}
}
3.使用MD5对SharedPreference的重要数据进行加密
嘿嘿,上面我们这样直接把

