自动登录
来源:互联网 发布:电梯编程视频教程 编辑:程序博客网 时间:2024/06/11 23:41
最近Android项目需要一个自动登录功能,完成之后,特总结一下,此功能依靠SharedPreferences进行实现。
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>
0 0
- 自动登录
- 自动登录
- 自动登录
- 自动登录
- 自动登录
- 自动登录
- 自动登录
- 自动登录yahoo邮箱
- WinXP、Win2003 自动登录
- 让XP自动登录
- 自动登录yahoo邮箱
- Windows2003自动登录
- WinXP实现自动登录
- 自动登录邮箱代码
- 自动登录Windows系统
- xp 自动登录
- 远程桌面+自动登录
- Windows系统自动登录
- [JIRA] 最新Linux版本 jira6.3.6安装汉化破解以及数据迁移
- 交通灯管理系统学习
- 数据结构 第一章 《思维导图》
- C++运算符重载讲解与经典实例(转自千里冰封网友)
- 封装一个字符数组,实现增加,删除,替换,打印,搜索等功能
- 自动登录
- Spark1.1.0 Transformations
- 设计模式-建造者模式给用户自由
- 《数据结构》 实验1
- Spark1.1.0 Actions
- 输入框弹出的时候内容上移动
- 绑定类修改
- k-means
- Tortoise SVN使用方法,简易图解(转)