我们在开发Android程序的时候,经常会遇到一些需要保存数据,以防下次再用到这些数据。
如果不是有接触,我想大家应该比较容易想到的是,数据库(SQLite数据库)和文件存储。其实,在android开发中,还有三种保存数据的方式,SharedPreferences存储、ContentProvider存储和Network存储。后两种,本篇博客不做详细介绍。
SharedPreferences保存的数据主要是类似配置信息格式的数据,因此它保存的数据主要是简单类型的key-value对。从用法角度来看,SharedPreferences和SharedPreferences.Editor组合起来非常像Map,SharedPreferences负责根据key读取数据,而SharedPreferences.Editor则用写入数据。
SharedPreferences的API超连接。。。。。。。。
SharedPreferences.Editor的API超连接。。。。。。
这里还有一个小问题要大家注意。那就是SharedPreferences存储数据时,只能保存一组数据。它可能有多个key-value对,不过不管它是怎么的多,都只能是一组数据。下面我就给出一个注册和登录小Demo中的关键代码来更好的说明一下。
注册(写入数据):
- public class RegistrationUI extends Activity {
-
- private String TAG = "RegistrationUI";
- SharedPreferences preferences;
- SharedPreferences.Editor editer;
-
- private EditText userNameEditTxt;
- private EditText userPwEditTxt1;
- private EditText userPwEditTxt2;
- private Button ok;
- private Button cancel;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.registration);
-
- preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);
- editer = preferences.edit();
-
- userNameEditTxt = (EditText) findViewById(R.id.reg_userNameEditTxt);
- userPwEditTxt1 = (EditText) findViewById(R.id.reg_userPwEditTxt1);
- userPwEditTxt2 = (EditText) findViewById(R.id.reg_userPwEditTxt2);
-
- ok = (Button) findViewById(R.id.determineBn);
- cancel = (Button) findViewById(R.id.cancelBn);
-
- ok.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");
- editer.putString("date_and_time", sdf.format(new Date()));
- String name = userNameEditTxt.getText().toString();
- String password1 = userPwEditTxt1.getText().toString();
- String password2 = userPwEditTxt2.getText().toString();
-
- if (password1.equals(password2))
- {
- editer.putString("userName", name);
- editer.putString("userPw", password1);
- editer.commit();
- }
- else
- {
- Toast.makeText(RegistrationUI.this, "前后两次输入的密码不一致,请重新输入。。。", Toast.LENGTH_SHORT);
- }
- }
- });
-
- cancel.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- }
- });
- }
- }
登录(读取数据):
- public class LoginUI extends Activity {
-
- SharedPreferences preferences;
- SharedPreferences.Editor editer;
-
- private EditText userName;
- private EditText userPw;
- private Button loginBn;
- private Button cancelBn;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
-
- preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);
-
- userName = (EditText) findViewById(R.id.login_userNameEditTxt);
- userPw = (EditText) findViewById(R.id.login_userPwEditTxt);
- loginBn = (Button) findViewById(R.id.login_determineBn);
- cancelBn = (Button) findViewById(R.id.login_cancelBn);
-
- loginBn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- String userName_DB = preferences.getString("userName", null);
- String userPw_DB = preferences.getString("userPw", null);
-
- if (userName_DB.equals(userName.getText().toString()) && userPw_DB.equals(userPw.getText().toString()))
- {
- Toast.makeText(LoginUI.this, "登录成功!", Toast.LENGTH_LONG).show();
- }
- else
- {
- Toast.makeText(LoginUI.this, "用户名或密码用误,请重新登录。", Toast.LENGTH_LONG).show();
- }
-
- }
- });
-
- }
- }
在这个例子中,大家可以看到,你在注册时填写的用户名和密码被保存到SharedPreferences中之后,我就可以用这个注册的用户名和密码来登录了。不过,要是用这个来实现注册和登录,只能是单用户的,因为上一次注册的账号会被下一次注册给覆盖掉。也就是说一台机子,一个程序只能跑一个用户。这样太不合理。所以这种存储数据的方式只能是保存一些配置信息(如是否打开音效,是否使用振动效果,小游戏的玩家积分等等)
这里也给出,我上传在CSDN上的程序源码:点击打开源码链接