SharedPreferences存储的两大特点是:存储形式是键值对、存储类型多样。下面通过例子说明:
1、从SharedPreferences中存储数据和读取数据
// MainActivity.java
private Button mButton_store;
private Button mButton_get;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton_store = (Button) findViewById(R.id.store_button);
mButton_get = (Button) findViewById(R.id.get_button);
mButton_store.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Jerry");
editor.putInt("age", 25);
editor.putBoolean("married", false);
Toast.makeText(MainActivity.this, "存储数据成功!", Toast.LENGTH_LONG).show();
}
});
mButton_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
int age = sharedPreferences.getInt("age", 0);
boolean married = sharedPreferences.getBoolean("married", false);
Log.e("sysu", "name is " + name);
Log.e("sysu", "age is " + age);
Log.e("sysu", "married is " + married);
}
});
}
效果:
注意:sharedpreferences.getXXX中第二个参数的意思是如果并没有存储以第一个参数为名的变量,那么就会返回第二个参数作为默认值。
但是在DDMS中没有发现xml文件,暂时不知道原因。
2、记住密码实现
修改上一章的BroadcastBestTest代码:
// LoginActivity.java
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.username_edit);
password = (EditText) findViewById(R.id.password_edit);
checkBox = (CheckBox) findViewById(R.id.remember_pass);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
login = (Button) findViewById(R.id.login_button);
login.setOnClickListener(loginListener);
boolean isChecked = sharedPreferences.getBoolean("isChecked", false);
if (isChecked) {
// 如果之前选中了记住密码,则把账号和密码设置到文本框中
String name = sharedPreferences.getString("username", "");
String pass = sharedPreferences.getString("password", "");
username.setText(name);
password.setText(pass);
checkBox.setChecked(true);
}
}
private OnClickListener loginListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
String name = username.getText().toString();
String pass = password.getText().toString();
if (name.equals(USERNAME) && pass.equals(PASSWORD)) { // 如果登录成功就跳转到MainActivity
// 现在先查看checkbox是否被选中,如果是,将账号和密码存储到sharedPreferences中
editor = sharedPreferences.edit();
if (checkBox.isChecked()) {
editor.putString("username", name);
editor.putString("password", pass);
editor.putBoolean("isChecked", true);
}
else {
editor.clear();
}
editor.commit();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(LoginActivity.this, "账户名或密码错误!", Toast.LENGTH_SHORT).show();
}
}
};
逻辑是不难理解的,这里要说明一下获取sharedPreferences的另一种方式:PreferenceManager.getDefaultSharedPreferences(this)。这是一个静态方法,它接收一个Context 参数,并自动使用当前应用程序的包名作为前缀来命名SharedPreferences 文件。