最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现。
https://blog.csdn.net/it_beecoder/article/details/77842448
SharedPreferences简介
SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
SharedPreferences使用实例:记住用户名密码自动登录
大致了解了SharedPreference之后,接下来看个记住用户名密码自动登录的例子:
- package com.dt5000.ischool.activity;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import com.dt5000.ischool.util.DTUtil;
- import com.dt5000.ischool.util.MyApplication;
- /**
- * @author: duanyr
- * @创建时间: 2012-11-13 下午2:36:47
- *
- * @类说明:登录界面
- */
- @SuppressLint("WorldReadableFiles")
- public class LoginActivity extends DTUtil {
- private static final String TAG = "用户登录";
- private EditText username;
- private EditText password;
- private CheckBox autoLogin;
- private SharedPreferences sharedPreferences;
- private String message;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyApplication.getInstance().addActivity(this);
- sharedPreferences = this.getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE);
- if (sharedPreferences.getBoolean("AUTO_ISCHECK", false)) {
- Intent intent = new Intent();
- intent.setClass(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- } else {
- setContentView(R.layout.activity_login);
- initView();
- }
- }
- /**
- * 初始化视图控件
- */
- public void initView() {
- Log.i(TAG, "初始化视图控件");
- username = (EditText) findViewById(R.id.username);
- password = (EditText) findViewById(R.id.password);
- autoLogin = (CheckBox) findViewById(R.id.autoLogin);
- // 默认记住用户名
- username.setText(sharedPreferences.getString("userName", ""));
- }
- /**
- * 点击登录按钮时触发的方法
- * @param view
- */
- public void userLogin(View view) {
- String usernameString = username.getText().toString();
- String passwordString = password.getText().toString();
- if (validateUser(usernameString, passwordString)) {
- Editor editor = sharedPreferences.edit();
- editor.putString("userName", usernameString);
- if (autoLogin.isChecked()) {// 自动登录
- editor.putString("password", passwordString);
- editor.putBoolean("AUTO_ISCHECK", true).commit();
- }
- editor.commit();
- Intent intent = new Intent();
- intent.setClass(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- } else {
- alert(this, message);
- }
- }
- //游客登录
- public void visitorLogin(View view) {
- Intent intent = new Intent();
- intent.setClass(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- }
- /**
- * 验证用户名密码是否正确
- *
- * @param username
- * @param password
- * @return
- */
- public boolean validateUser(String username, String password) {
- Boolean flag = false;
- try {
- //...此处为调用web服务,验证用户名密码的服务,特此省略
- flag = true;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- message = "连接服务器失败";
- }
- return flag;
- }
- /**
- * 点击退出按钮时触发的方法
- */
- public void logout_listener(View view) {
- dialog_Exit(this);
- }
- /**
- * 监听返回按钮,此为登录界面再返回的话给出退出提示
- */
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
- dialog_Exit(this);
- return false;
- }
- return false;
- }
- }
页面布局截图:
生成的配置文件位置和代码
userInfo.xml的具体代码如下:
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <string name="userName">777</string>
- <string name="password">111111</string>
- <boolean name="AUTO_ISCHECK" value="true" />
- </map>